这是我正在使用的代码:
import discord
import random
import asyncio
from discord.ext import commands
from discord.ext.commands import bot
from discord.utils import get
def IsTeam(role):
Value = False
AllTeams = [] # Add all teams in here.
if role in AllTeams:
Value = True
return Value
@bot.command()
async def sign(ctx, member: discord.Member=None, *, arg=None):
if not member:
await ctx.send("Please provide a username.")
elif arg == None:
await ctx.send("Please provide a team.")
else:
try:
Role = discord.utils.get(ctx.guild.roles, name=arg)
if Role == None:
await ctx.send("This is an invalid role.")
else:
pass
except:
await ctx.send("Unexpected Error Occured.")
return
if Role >= ctx.author.top_role:
await ctx.send("You cannot add a role that is higher then your current role.")
else:
if IsTeam(Role):
if Role in ctx.author.roles:
await member.add_roles(Role)
await ctx.send(f'{member.mention} has been signed to `{role}`')
我想知道我是否可以做
@bot.command()
async def sign(ctx, member: discord.Member=None, *, arg=None):
命令输入事件on_message,例如:
@bot.event()
async def on_message(ctx, member: discord.Member=None, *, arg=None):
同时执行命令所执行的相同操作。 抱歉,如果我没有清楚地说明这一点。
答案 0 :(得分:0)
据我了解,当某人用特定的前缀提及用户时,您正在尝试赋予该角色。在这种情况下,您的前缀为emoji+ sign
。您可以为此使用on_message
事件。但是在on_message
事件中,您只能使用message
参数。另外,您还需要通过在不和谐聊天中输入\:TeamEmoji:
来获取表情符号ID。它将返回类似<:TeamEmoji:123456789>
的内容。因此,您基本上可以做到:
@bot.event
async def on_message(message):
if message.content.startswith('<:TeamEmoji:1234567890> sign'):
role = discord.utils.get(message.guild.roles, name='role name')
member = message.mentions[0]
await member.add_roles(role)
await message.channel.send('Successfully signed to <:TEAM:123456789>')