我正在使用 Discord 机器人制作“米勒斯山谷的狼人”游戏。为此我设置了9个角色,分配给每个玩家,每个角色代表一个角色,这样玩家就不会知道并且可以访问不同的频道。但我遇到了这样的问题:Discord,即使我在范围中指定它(机器人是管理员),在覆盖权限中指定,它给出:
'403 禁止(错误代码:50013):缺少权限'
完整输出:
2024-10-25 00:03:37 INFO discord.client logging in using static token
2024-10-25 00:03:38 INFO discord.gateway Shard ID None has connected to Gateway (Session ID: 9ffd92e484c54602bf4d2fcd50893267).
Creating roles
Deleting roles on users
[<Role id=1195679159821799444 name='@everyone'>, <Role id=1239274286880133123 name='Machine_control'>, <Role id=1220862610266783806 name='9'>, <Role id=1220862606789836831 name='8'>, <Role id=1220862604390432778 name='7'>, <Role id=1220862584001925193 name='6'>, <Role id=1220862581753909288 name='5'>, <Role id=1220862578280890420 name='4'>, <Role id=1220862575957246072 name='3'>, <Role id=1220862573004460177 name='2'>, <Role id=1220862570022436914 name='1'>, <Role id=1299129640807432195 name='DisDawn'>]
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
Attributing roles
[<Role id=1195679159821799444 name='@everyone'>, <Role id=1239274286880133123 name='Machine_control'>, <Role id=1220862610266783806 name='9'>, <Role id=1220862606789836831 name='8'>, <Role id=1220862604390432778 name='7'>, <Role id=1220862584001925193 name='6'>, <Role id=1220862581753909288 name='5'>, <Role id=1220862578280890420 name='4'>, <Role id=1220862575957246072 name='3'>, <Role id=1220862573004460177 name='2'>, <Role id=1220862570022436914 name='1'>, <Role id=1299129640807432195 name='DisDawn'>]
[<Role id=1220862610266783806 name='9'>, <Role id=1220862570022436914 name='1'>, <Role id=1220862581753909288 name='5'>, <Role id=1220862606789836831 name='8'>, <Role id=1220862578280890420 name='4'>, <Role id=1239274286880133123 name='Machine_control'>, <Role id=1220862584001925193 name='6'>, <Role id=1220862575957246072 name='3'>, <Role id=1220862604390432778 name='7'>, <Role id=1220862573004460177 name='2'>]
ERROR:root:
Traceback (most recent call last):
File "c:\Users\stard\OneDrive\Documents\code\Digi\Discord bot\v2\BOT.py", line 128, in start_game
await game.assign_roles(ctx)
File "c:\Users\stard\OneDrive\Documents\code\Digi\Discord bot\v2\Werewolf_game.py", line 104, in assign_roles
await player.discord.add_roles(r)
File "C:\Users\stard\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\discord\member.py", line 1051, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "C:\Users\stard\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\discord\http.py", line 739, in request
raise Forbidden(response, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
这是导致错误的代码:
async def reset(self, ctx: discord.ext.commands.Context):
print("Deleting roles on users")
overwrite = discord.PermissionOverwrite()
overwrite.view_channel = False
roles = list(ctx.guild.roles)
print(roles)
for element in roles:
if element.name not in ["1","2","3","4","5","6","7","8","9"]:
roles.remove(element)
for role in roles:
await self.werewolf_channel.set_permissions(role, overwrite=overwrite)
await self.specials_channel.set_permissions(role, overwrite=overwrite)
for user in self.player_list:
for role in roles:
try:
await user.discord.remove_roles(role)
except Exception as error:
print(error)
async def assign_roles(self, ctx: discord.ext.commands.Context):
print("Attributing roles")
werewolf_channel = self.werewolf_channel
specials_channel = self.specials_channel
roles = list(ctx.guild.roles)
print(roles)
for element in roles:
if element.name not in ["1","2","3","4","5","6","7","8","9"]:
roles.remove(element)
random.shuffle(roles)
print(roles)
for index, player in enumerate(self.player_list):
r = roles[index]
if player.role == "werewolf":
await player.discord.add_roles(r)
overwrite = discord.PermissionOverwrite()
overwrite.read_message_history = False
await werewolf_channel.set_permissions(r, overwrite=overwrite)
if player.role in ["president", "cupidon", "hunter", "witch", "stealer"]:
await player.discord.add_roles(r)
overwrite = discord.PermissionOverwrite()
overwrite.read_message_history = False
await specials_channel.set_permissions(r, overwrite=overwrite)
else:
await player.discord.add_roles(r)
首先,我验证了范围是否正确,它们是(在法语中,它表明机器人是管理员+具有管理角色)。
然后,我读到机器人的角色必须高于它给出的角色,这也验证了(该机器人称为 DisDawn)...
代码中也有指定:
intents = discord.Intents.default()
intents.message_content = True
intents.reactions = True
intents.members = True
overwrite = discord.PermissionOverwrite()
overwrite.read_messages = False
overwrite.manage_channels = True
overwrite.manage_roles = True
bot = commands.Bot(command_prefix="!", intents=intents, overwrite=overwrite)
完整代码可在此处获取:https://github.com/sachomatic/Disbot/tree/master
如何修复这个错误?
我找到了为什么我得到代码 403。由于算法错误,我试图为用户提供机器人角色 (Machine_control):`async def get_game_roles(self,ctx:discord.ext.commands.Context): 角色=列表(ctx.guild.roles)
for role in roles:
print(role)
if role.name not in ["1","2","3","4","5","6","7","8","9"]:
roles.remove(role)
#print(f"Removed role {role} from roles")
print(f"Keeping roles :")
for prnt_role in roles:
print(prnt_role.name,end=", ")
random.shuffle(roles)
print("")
return roles`
所以它返回[1,2,3,4,5,6,7,8,9,Machine_Control]。