我正在尝试通过一个简单的用法为我的不和而创建一个机器人:通过在该消息上添加反应来对消息中的触发短语做出反应,只有当发送消息的用户具有某个角色时才会如此。
这是工作代码:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix='!')
default_emojis = [
]
custom_emojis = [
"war_tank",
"war_dps",
"SP",
"DP",
"r_drood",
"feral",
"equi",
"drood_tank",
"cham_elem",
"cham_amelio",
"r_cham",
"rogue",
"demono",
"mage",
"hunt",
"DK_DPS",
"DK_Tank",
"pal_prot",
"pal_ret",
"h_pal"
]
async def react(message):
for emoji in default_emojis:
await message.add_reaction(emoji)
for emoji in message.guild.emojis:
if emoji.name in custom_emojis:
await message.add_reaction(emoji)
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if "indiquez votre spé" in message.content.lower():
await react(message)
截至目前,这是为了“在触发短语发送反应”部分,但我无法弄清楚如何检查用户的角色。
目前的工作代码已由其他人提供,因为我正在努力编码。我想要一些简单的东西,并认为我对编程的微薄知识就足够了,但这是一种我不太了解的语言,而且我无法理解Discord API如何解决我的问题。
因此,如果有人知道如何在盲目地对消息作出反应之前添加一些可以检查角色的内容,那就太棒了。
感谢您的阅读和时间。
您可以使用discord.utils.get
根据特定条件检查用户角色。如果任何角色符合这些条件,将返回它,否则它将返回None
。
from discord.utils import get
if get(message.author.roles, name="Role name") and "indiquez votre spé" in message.content.lower():
await react(message)
首先找到服务器然后找到角色。你必须将它们放在异步函数中,否则它将无法工作,这就是它的工作原理。
希望这是你想要的帮助。
import discord
from discord.ext import commands
from discord.utils import get,find
bot=commands.Bot(command_prefix='.')
role=[]
async def info():
server=get(bot.servers,name='Yuno')
rol=get(server.roles,name='Admin')
role.append(rol)
@bot.event
async def on_ready():
await info()
print(bot.user.name)
@bot.event
async def on_message(msg):
if role[0] in msg.author.roles:
await bot.add_reactions(msg,'emoji')
bot.run('')