繁体   English   中英

在 cogs discord.py 中的特定通道中发送消息

[英]Send Message In Specific Channel In cogs discord.py

我试图在特定频道中发送消息...我知道如何正常指定频道,但我在 cogs 中变得非常困惑我真的无法理解如何这样做....任何帮助表示赞赏:)

@commands.Cog.listener()
    async def on_ready(ctx, channel:discord.Channel=None):
        channel = '785465306470547457'
        role = discord.utils.get(user.server.roles, name="Orange")
        message = await ctx.send_message(channel, "React to me!")
        while True:
            reaction = await ctx.wait_for_reaction(emoji="🟠", message=message)
            await ctx.add_roles(reaction.message.author, role)

错误

discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.rolesystem' raised an error: AttributeError: module 'discord' has no attribute 'Channel'
  1. 在类中(除非它是staticmethodclassmethod )函数总是将self作为第一个参数
  2. on_ready不带任何 arguments
  3. 它是discord.TextChannel而不是discord.Channel也仅在命令中使用它
  4. ID 是整数,通道必须是abc.Messageable object 不是string
  5. 您没有定义userserver也已在1.0.0版本中重命名为guild
  6. 这是ctx.send不是ctx.send_message
  7. 这是bot.wait_for不是ctx.wait_for_reaction

另外,while循环应该做什么?

@commands.Cog.listener()
async def on_ready(self):
    """This will wait for someone to react with `🟠`
    and add him a role
  
    Note: This will wait only for one reaction"""

    channel_id = 785465306470547457 
    # Getting the channel
    channel = self.bot.get_channel(channel_id)
    # Getting the role
    role = discord.utils.get(channel.guild.roles, name='Orange')
    # Sending the message
    message = await channel.send('React to me')

    def check(reaction, user):
        """Checks if the reaction message is the one sent before
        and if the reaction is `🟠`"""
        return reaction.message == message and str(reaction) == '🟠'

    # Waiting for the reaction
    reaction, user = await self.bot.wait_for('reaction_add', check=check)
    # Adding the role
    await user.add_roles(role)

注意:除非您启用默认意图和意图,否则此代码不起作用。 intents.members 这里是对意图的介绍

阅读文档,它真的很有帮助,并在深入了解pythonOOP之前了解更多信息discord.py

另外,从代码中,我想您想发送一条 static 消息,当有人做出反应时,为他添加一个角色,为此使用on_raw_reaction_add事件

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM