[英]Discord.py - messages for users with more roles
我希望我的机器人向具有两个或多个特定角色的用户写一条消息,而不仅仅是其中的一部分。
代码在这里(没有令牌):
import discord
import asyncio
client = discord.Client()
@client.event
async def on_message(message):
member = message.author
role = discord.utils.get(message.guild.roles, name="2A") and member.roles=="1B", "1C", "1D", "1E", "1F", "2B", "2C", "2D", "2E", "2F", "3A", "3B", "3C", "3D", "3E", "3F", "4A", "4B", "4C", "4D", "4E", "4F", "5A", "5B", "5C", "5D", "6A", "6B", "6C", "6D", "7A", "7B", "7C", "7D", "8A", "8B", "8C", "8D"
if role in message.author.roles and message.content == "test":
await message.channel.send("response")
else:
return
role = discord.utils.get(message.guild.roles, name="3A") and member.roles=="1B", "1C", "1D", "1E", "1F", "2B", "2C", "2D", "2E", "2F", "3B", "3C", "3D", "3E", "3F", "4A", "4B", "4C", "4D", "4E", "4F", "5A", "5B", "5C", "5D", "6A", "6B", "6C", "6D", "7A", "7B", "7C", "7D", "8A", "8B", "8C", "8D"
if role in message.author.roles and message.content == "test":
await message.channel.send("response")
else:
return
但它没有任何反应。 甚至可以编写这样的程序吗?
因此,您可以制定一个更简单的解决方案,而不仅仅是通过保存名称来获取每个角色。 我宁愿用 ID 来做这件事,因为它们总是一样的
然后您可以删除member
变量,因为它非常无用(您只是缩短了一个短名称)
然后检查用户必须查看的所有角色,看看它们中的任何一个是否在名称列表中(并且内容是“测试”)。
编辑:您必须仔细检查角色,因为您想知道角色中是否有两个role_names
角色,您也可以将if message.content...
移到顶部以保存每次遍历所有角色消息已发送
如果某个角色在名单中,你必须break
for loop
并继续做你之后做的事情(如果之后没有任何事情,你也可以删除else: return
)
最后看起来像这样:
import discord
import asyncio
role_names = ["1B", "1C", "1D", "1E", "1F", "2A", "2B", "2C", "2D", "2E", "2F", "3A", "3B", "3C", "3D", "3E", "3F", "4A", "4B", "4C", "4D", "4E", "4F", "5A", "5B", "5C", "5D", "6A", "6B", "6C", "6D", "7A", "7B", "7C", "7D", "8A", "8B", "8C", "8D"]
client = discord.Client()
@client.event
async def on_message(message):
if message.content == "test":
role1 = None
for role in message.author.roles:
if role.name in role_names:
role1 = role
if role1 is not None and role2.name in role_names and role2.name != role1.name:
# Check if role1 is found and role2 has another role name and is a right role
await message.channel.send("response")
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.