При создании бота Discord на Python с использованием discord-ui и discord.py я хотел создать команду, которая выполняет определенные действия для определенных ролей. Например, если у меня есть att_role и aot_role, он выполнит код для обеих этих ролей, а если я добавлю еще одну роль, например est_role, он также выполнит свой код. В качестве IDE я использую PyCharm.
Я начинающий программист, поэтому не знаю, лучший ли это способ сделать это, но когда я это делаю, я получаю ошибка «Приложение не ответило». Вот когда это работает, а когда нет:
Когда у меня есть att_role, он работает отлично, даже если я добавляю больше ролей из списка. Если я удалю att_role и добавлю две другие роли из списка, для них это тоже будет работать. Если у меня есть только rs_role, это тоже сработает. Однако, если у меня есть только aot_role или только est_role, после запуска я получаю эту ошибку: - Приложение не ответило на канале разногласий. Если я добавлю к ним какую-либо другую роль, все будет работать отлично. В моей IDE (PyCharm) не отображается никаких ошибок.
Если вы не правильно понимаете, что я имею в виду, просто скажите мне, как лучше всего делаю это: делаю разные вещи для разных ролей.
Вот мой код:
import discord
from discord.ext import commands
from discord_ui import UI, SelectOption, SelectMenu
import asyncio
# Clients
client = commands.Bot(' ')
client.remove_command('help')
ui = UI(client)
@ui.slash.command()
async def log(ctx):
"""Use this command to log trainings/tryouts"""
# These are the roles allowed to use this command
allowed_roles = [
'Advanced Trooper Training Section',
'Advanced Officer Training Section,',
'Recruiting Staff',
'Academy Instructor'
'Executive Sergeant Training Section',
'TB Bot Manager'
]
author_roles = ctx.author.roles
# If statement to check if the user can use this command
if any(role.name in allowed_roles for role in author_roles):
att_role = discord.utils.get(author_roles, name='Advanced Trooper Training Section')
aot_role = discord.utils.get(author_roles, name='Advanced Officer Training Section')
rs_role = discord.utils.get(author_roles, name='Recruiting Staff')
ai_role = discord.utils.get(author_roles, name='Academy Instructor')
est_role = discord.utils.get(author_roles, name='Executive Sergeant Training Section')
manager_role = discord.utils.get(author_roles, name='TB Bot Manager')
# Different stuff for different roles
if att_role is not None and att_role.name == 'Advanced Trooper Training Section':
await ctx.send('att') # Sample Stuff
if rs_role is not None and rs_role.name == 'Recruiting Staff':
await ctx.send('rs') # Sample Stuff
if ai_role is not None and ai_role.name == 'Academy Instructor':
await ctx.send('ai') # Sample Stuff
if est_role is not None and est_role.name == 'Executive Sergeant Training Section':
await ctx.send('est') # Sample Stuff
if manager_role is not None and manager_role.name == 'TB Bot Manager':
await ctx.send('manager') # Sample Stuff
if aot_role is not None and aot_role.name == 'Advanced Officer Training Section':
await ctx.send('aot') # Sample Stuff
client.run(TOKEN)
Подробнее здесь: https://stackoverflow.com/questions/709 ... sing-disco