首页 > 解决方案 > 我想让这段代码只针对一个角色,我该怎么做?

问题描述

with open('database.txt') as file:
    file = file.read().split()

@client.event 
async def on_message(message):
    for database in file:
        if database in message.content.lower():
            await message.channel.send(f'{message.author.mention}**! Daha hızlı banlanması için <@854087258990444564> Botuna DM At** !')

我想让这段代码只针对一个角色,我该怎么做?

标签: pythondiscord.py

解决方案


您只需要检查作者是否具有角色,然后才能执行命令。


@client.event 
async def on_message(message):
    role = discord.utils.get(message.guild.roles, name='role_name')
    if role in message.author.roles:
       # command here

这是我能想到的最简单的方法。但是我强烈建议使用 discord.commands 框架来制作机器人。

参考:


推荐阅读