使discord.py机器人只处理特定角色的私信消息

huangapple go评论80阅读模式
英文:

Getting discord.py bot to only process dms from certain role

问题

抱歉,你的要求是只返回翻译好的部分,没有其他内容。以下是代码部分的翻译:

@bot.event
async def on_message(message):
    if message.author == bot.user or message.author.id in ignoreusers:
        return
    if isinstance(message.channel, discord.DMChannel):
        channel = bot.get_channel(1118363617654472805)
        await channel.send(f"{message.author} 发送了消息:\n{message.content}")
    await bot.process_commands(message)

如果你有任何其他问题或需要进一步的帮助,请随时提出。

英文:

Hey there I am incredibly new to coding in python I have a little bot written that takes dms and outputs them into a certain channel with a simple ignore function for user ids if they get banned from the server so they cannot message anymore. I was wondering if there was a way I could add to the code a way for the bot to only receive dms from a role.

@bot.event
async def on_message(message):
    if message.author == bot.user or message.author.id in ignoreusers:
        return
    if isinstance(message.channel, discord.DMChannel):
        channel = bot.get_channel(1118363617654472805)
        await channel.send(f"{message.author} sent:\n{message.content}")
    await bot.process_commands(message)

I don't even know where to begin

答案1

得分: 1

你可以使用discord.utils来创建一个条件,只有当发送者拥有特定角色时才执行特定操作。在我的示例中,我正在验证发送消息的用户是否具有角色"MOD"。

role = discord.utils.get(message.guild.roles, name="MOD")
if role in message.author.roles:
    # 执行某些操作
else:
    # 执行其他操作
英文:

You can make a condition with discord.utils to do an specific action only if the sender has that role. Here in my example, I'm validating if the user that sent the message has the role "MOD".

role = discord.utils.get(message.guild.roles, name="MOD")
if role in message.author.roles:
      Do something
    else:
    Do something else

huangapple
  • 本文由 发表于 2023年7月11日 01:56:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定