【问题标题】:How do I give a role to someone in discord.py?如何在 discord.py 中给某人一个角色?
【发布时间】:2021-10-28 08:36:19
【问题描述】:

我整晚都在尝试编写一个单一的反应角色,但我无法让代码工作,机器人会对消息和所有内容做出反应,但当我做出反应时它没有给我角色

这是我的代码

@bot.command(name='rolecreate', help='creates all the default roles')
async def rolecreate(ctx):
  Text= "React with :heart: to get the He/Him role!"
  Moji1 = await ctx.send(Text)
  reaction='❤️'
  await Moji1.add_reaction(reaction)
async def on_reaction_add(reaction, member, ctx):
    Channel = bot.get_channel('881380132789583892')
    if reaction.message.channel.name!= Channel:
          return
          if reaction.emoji == "❤️":
            guild= ctx.guild
              he_him = discord.utils.get(guild.roles, name="He/Him")

          if not he_him:
            he_him = await guild.create_role(name="He/Him")

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python discord bots roles


【解决方案1】:

我认为您应该将 if reaction.emoji == "❤️": 之后的代码从父 if 语句中取出。似乎第一个 if 语句是我们不想要的行为,其余代码属于不同的逻辑路径。 return 之后的任何代码都不会被执行。

你可以试试这个吗?

@bot.command(name='rolecreate', help='creates all the default roles')
async def rolecreate(ctx):
  Text= "React with :heart: to get the He/Him role!"
  Moji1 = await ctx.send(Text)
  reaction='❤️'
  await Moji1.add_reaction(reaction)
async def on_reaction_add(reaction, member, ctx):
    Channel = bot.get_channel('881380132789583892')
    if reaction.message.channel.name!= Channel:
        return
    if reaction.emoji == "❤️":
        guild= ctx.guild
        he_him = discord.utils.get(guild.roles, name="He/Him")

        if not he_him:
            he_him = await guild.create_role(name="He/Him")

【讨论】:

  • 不,这不起作用,机器人仍然会发送消息,但是当我点击反应时,它并没有给我角色
猜你喜欢
  • 2020-09-26
  • 2019-10-28
  • 2021-10-19
  • 2018-09-16
  • 1970-01-01
  • 2021-04-16
  • 2018-04-15
  • 1970-01-01
  • 2020-04-21
相关资源
最近更新 更多