【问题标题】:Send a message to a channel using a command discord.py使用命令 discord.py 向频道发送消息
【发布时间】:2021-03-24 01:52:57
【问题描述】:

我正在尝试使用代码为我的不和谐机器人创建一个-say #channel-name message 命令:

@commands.command()
@commands.has_permissions(manage_messages=True)
async def say(self, ctx, channel, *, message):
    channel = discord.
    await channel.send(message)

出现错误
discord.ext.commands.errors.CommandInvokeError: CommandAttributeError: 'str' object has no attribute 'send'
据我所知,这段代码是正确的,应该能够向提到的频道发送消息,但它没有,而是抛出错误。

我做错了什么?

【问题讨论】:

  • 您是要向执行命令的通道还是用户指定的通道发送消息?
  • 什么是discord.?您可能可以完全删除该通道参数并将底线更改为await ctx.send(message)。除非用户输入通道参数以将输出消息定向到另一个通道,而不是使用它的通道?
  • 如果您希望用户自己定义频道,请使用:discord.utils.get(self.get_guild(ctx.guild.id).channels, name=channel) 同时检查您的缩进。这里看起来不合适,但在您的实际脚本中可能是正确的

标签: python python-3.x discord discord.py


【解决方案1】:

channel 的类型是 str,而不是 discord.TextChannel。要将其转换为通道,您可以在参数中执行channel : discord.TextChannel

@commands.command()
@commands.has_permissions(manage_messages=True)
    async def say(self, ctx, channel : discord.TextChannel, *, message):
    channel = discord.
    await channel.send(message)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2021-05-17
    • 2020-12-03
    相关资源
    最近更新 更多