discord.py如何为用户v1.0版本分配角色

时间:2020-06-04 11:06:43

标签: python python-3.x discord discord.py discord.py-rewrite

class User(Base):
    name = Column(String)
    email = Column(String)
    is_hidden = Column(Boolean)

user_filter = query(User).filter(User.is_hidden == True)

user1 = User(name="username", email="username@mail.com", is_hidden=True)

is_filter_applies_on_object(user_filter, user1)  # should return True

这是我用来尝试为输入命令的人员分配角色的代码。我的服务器上可用的角色是rs6,因此代码应为def recency(config): i = config.exclude_list sales = pd.read_excel(config.input_path + "klm sales UPD.xlsx") df_exclude = sales[(sales["Product"].str.contains(i[0] + '|' i[1])==True) | (sales["Customer"].str.contains(i[2] + '|' + i[3] + '|' + i[4] + '|' + i[5] + '|' + i[6])==True) | (sales["Remarks"].str.contains(i[7])== True) | (sales["Net Amount"]<10)] ,但不起作用。

import discord
import logging
from discord.utils import get
from discord.ext import commands
logging.basicConfig(level=logging.INFO)
bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')
@bot.command(pass_context=True)
async def setrole(ctx, a: str):
    member = ctx.message.author
    role = discord.utils.get(member.guild.roles, name=a)
    await member.add_roles(member, role)

我尝试在第!setrole rs6行之后执行Ignoring exception in command setrole: Traceback (most recent call last): File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped ret = await coro(*args, **kwargs) File "C:\Workshop\example_bot.py", line 19, in setrole await member.add_roles(member, role) File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py", line 641, in add_roles await req(guild_id, user_id, role.id, reason=reason) File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\http.py", line 223, in request raise NotFound(r, data) discord.errors.NotFound: 404 Not Found (error code: 10011): Unknown Role The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke await ctx.command.invoke(ctx) File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke await injected(*ctx.args, **ctx.kwargs) File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10011): Unknown Role 并打印出正确的rs6。有人请帮助我!谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用命令装饰器来做一件令人眼花thing乱的事情。与将arg的类型设置为str的方式相同,也可以将其设置为不和谐的对象:

@bot.command() # Note that context is automatically passed in rewrite
async def setrole(ctx, role: discord.Role):
    await ctx.author.add_roles(role)
    await ctx.send(f"I gave you {role.mention}!")

如果找不到该角色,您可能希望使用错误处理程序进行处理:

@setrole.error
async def _role_error(ctx, error):
    if isinstance(error, commands.errors.BadArgument):
        await ctx.send("I couldn't find that role!")

参考: