简体   繁体   中英

Send Message In Specific Channel In cogs discord.py

im trying to send a message in specific channel...i know how to specify channel normally but im getting very confused in cogs im really not able to understand how to do so....any help is appreciated:)

@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)

ERROR

discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.rolesystem' raised an error: AttributeError: module 'discord' has no attribute 'Channel'
  1. In classes (unless it's a staticmethod or classmethod ) functions always take self as the first argument
  2. on_ready doesn't take any arguments
  3. It's discord.TextChannel not discord.Channel also use this only in commands
  4. ID's are integers and channels must be abc.Messageable object not string
  5. You didn't define user , also server has been renamed to guild in version 1.0.0
  6. It's ctx.send not ctx.send_message
  7. It's bot.wait_for not ctx.wait_for_reaction

Also, what's the while loop suppose to do?

@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)

Note: This code will not work unless you enable default intents and intents.members here's an introduction to intents.

Please read the documentation , it's really helpful and learn more python and OOP before diving into discord.py .

Also, from the code, I suppose you want to send a static message, and when someone reacts, add him a role, use the on_raw_reaction_add event for that

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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