英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论