python - discord.py 错误,同时给一个人一个静音的角色
问题描述
我正在尝试制作一个具有某些功能的不和谐机器人。其中一个包括一个静音功能,由于某种原因对我来说不起作用
代码
@bot.command(pass_context=True)
@commands.has_role("gl3tche")
async def mute(ctx, member:discord.Member, *, reason = None):
await ctx.channel.purge(limit=1)
print(member)
if not member:
await ctx.channel.send(embed=Embed(title="Mute").add_field("*mute {user}", "Mutes the user"))
else:
#member = ctx.author
try:
role = discord.utils.get(ctx.guild.roles, name="muted")
except:
await ctx.guild.create_role(name="muted")
role = discord.utils.get(ctx.guild.roles, name="muted")
#perms = discord.Permissions()
#perms.update(read_messages = True, read_message_history = True, connect = True, speak = False, send_messages = False)
await member.add_roles(role)
await member.add_roles(role)
await ctx.channel.send(f"{member.mention} has been muted")
我得到的错误:
Ignoring exception in command mute:
Traceback (most recent call last):
File "C:\Users\saketh\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\saketh\OneDrive\Documents\Code\Python\Bots\BU\NewBot.py", line 89, in mute
await member.add_roles(role)
File "C:\Users\saketh\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 777, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\saketh\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\saketh\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\saketh\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'id'
有人能帮助我吗?将不胜感激:D
解决方案
看用户端
成员:discord.Member
推荐阅读
- c++ - 交友类,而交友类里面有交友类的对象
- ios - Xcode 12 推送通知切换为空
- ios - Swift - #selector 方法没有被调用
- sql-server - 不同 Top 计数的 SQL Server 执行计划
- vue.js - Vuex状态数组在变异时转换代理对象
- visual-studio - 部署 SSIS 包时 Visual Studio 2012 崩溃
- r - 从 Reshape 转换为 Reshape 2 时融化不起作用
- c# - 当我将我的程序放入不同的方法中,并将它们组合成一个循环时,它们不能正确地相互读取。有谁知道为什么?
- flutter - Flutter,如何从子级更改当前标签栏视图或页面或索引
- google-cloud-platform - Google Cloud IoT - 注册表中所有设备的单个 MQTT 客户端实例