atm 我正在将所有 app_commands 收集到 cogs 上以重新排序所有内容。
随着
pairings.py
我遇到了困难,因为“get_member_named”不是“None”的已知成员,在cogs.app_commands上显示错误,而外部齿轮工作得很好!
齿轮之外的pairings.py第一个版本运行完美,并且是一样的。不幸的是,如果我尝试在齿轮之外运行代码,它就不再起作用了。
这是cog.pairings:
import discord
import settings
import typing
from discord.ext import commands
from discord import app_commands
from typing import Optional
from io import BytesIO
from datetime import datetime
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix ='!', intents = intents)
class Pairings(commands.Cog):
def __init__(self, bot):
self.bot = bot
@app_commands.command()
@app_commands.describe(torneo = "Quale Torneo")
@app_commands.describe(turni = "Quale Turno")
@app_commands.describe(start = "A che ora parte il Turno da comunicare")
@app_commands.describe(bye = "Se c'è il Bye")
@app_commands.checks.has_any_role("ADMIN", "CONFERMATI")
async def pairings(self, interaction : discord.Interaction,
torneo : typing.Literal['Primo Torneo', 'Secondo Torneo'],
turni : typing.Literal['Turno 1', 'Turno 2', 'Turno 3', 'Turno 4', 'Turno 5'],
start : str,
bye : Optional[typing.Literal['Si', 'No']],
player_con_bye : Optional[str],
):
if bye == 'Si':
guild = self.bot.get_guild(int(1156534000890953748))
players = discord.utils.get(guild.roles, id=1156535561901838396)
pairing_ch = self.bot.get_channel(int(1156534713805180989))
bye_player = interaction.guild.get_member_named(player_con_bye)
start_turno = datetime.strptime(start, '%H:%M')
start_turno_convertito = datetime.strftime(start_turno, '%H:%M')
await interaction.response.send_message("Oki,Doki. Ora inviami l'immagine dei pairings!", ephemeral=True)
message = await self.bot.wait_for('message')
pairings_img = message.attachments[0]
img_bytes = await pairings_img.read()
await pairing_ch.send(f"{players.mention} \n\n**{torneo}** \n**{turni}** \n**Bye:** {bye_player.mention} \n**{start_turno_convertito}**")
await pairing_ch.send(file=discord.File(fp=BytesIO(img_bytes), filename=pairings_img.filename))
else:
guild = self.bot.get_guild(int(1156534000890953748))
players = discord.utils.get(guild.roles, id=1156535561901838396)
pairing_ch = self.bot.get_channel(int(1156534713805180989))
start_turno = datetime.strptime(start, '%H:%M')
start_turno_convertito = datetime.strftime(start_turno, '%H:%M')
await interaction.response.send_message("Oki,Doki. Ora inviami l'immagine dei pairings!", ephemeral=True)
message = await self.bot.wait_for('message')
pairings_img = message.attachments[0]
img_bytes = await pairings_img.read()
await pairing_ch.send(f"{players.mention} \n\n**{torneo}** \n**{turni}** \n**{start_turno_convertito}**")
await pairing_ch.send(file=discord.File(fp=BytesIO(img_bytes), filename=pairings_img.filename))
@pairings.error
async def on_error(self, interaction: discord.Interaction, error):
if isinstance(error, app_commands.MissingAnyRole):
await interaction.response.send_message("Non hai i permessi per usare questo comando!", ephemeral=True)
async def setup(bot):
await bot.add_cog(Pairings(bot))
这是 main.py
import discord
import settings
import typing
from cogs.apertura_chiusura import Apertura_Chiusura
from cogs.iscrizioni import Iscrizione
from datetime import datetime
from discord import app_commands
from discord.ext import commands
from io import BytesIO
from typing import Optional
logger = settings.logging.getLogger("bot")
def run():
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix ='!', intents = intents)
#ON READY
@bot.event
async def on_ready():
logger.info(f"User: {bot.user.display_name} (ID: {bot.user.id})")
bot_name = await bot.fetch_user(int(1155959555876991098))
for cog_file in settings.COGS_DIR.glob("*.py"):
if cog_file.name != "__init__.py":
await bot.load_extension(f"cogs.{cog_file.name[:-3]}")
await bot.tree.sync()
print(f"SUUUUUUPER! SIAMO ON!")
bot.run(settings.TOKEN, root_logger=True)
run()
我想获取
player_con_bye
的交互输入,它应该始终是 app_commands.user 试图实现的成员的正确昵称。
由于之前的选择,此选择是可选的
bye
此后,该命令应发送一条包含所有选择的消息,并提及再见的用户。
谢谢!
正如猜测的那样,关闭机器人后,等待其离线并重新启动,pairings.py 工作完美。
对于类型检查仍然存在上述错误。
如果有人知道如何使用正确的字典进行修复,请告诉我!