簡體   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