我正在尝试使用discord.py库将消息从一个 channel 发送到另一个 channel 。想法 - channel _1 用户无权读取和发送 channel _2 中的消息。我尝试编写应该发送这些消息的机器人 - 例如,用户编写!发送“channel2”“hello”,机器人将此消息发送到 channel 2。但我在尝试执行此操作时遇到错误
import os
import random
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
token = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
async def xsend(ctx, *, message):
await bot.delete_message(ctx.message)
await ctx.send(discord.Object(id='652024045339934731'), message)
bot.run(token)
我得到的错误 - TypeError: send() 采用 1 到 2 个位置参数,但给出了 3 个
最佳答案
这不是discord.py-rewrite,对吧?因此,只需使用 bot.get_channel() 并通过 bot.send_message() 发送消息即可。 Link to documentation
(顺便说一句,ctx.send() 会将消息发送到调用的 channel ,如果我知道的话)
@bot.command(pass_context=True)
async def xsend(ctx, *, message: str):
await bot.delete_message(ctx.message)
channel = bot.get_channel('652024045339934731')
if channel:
await bot.send_message(channel, message)
(discord.py-rewrite 版本)
@bot.command(pass_context=True)
async def xsend(ctx, *, message: str):
await ctx.message.delete()
channel = bot.get_channel(652024045339934731)
if channel:
await channel.send(message)
关于python - Discord py 向 channel 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59333157/