環境
Python 3.9.13, discord.py 2.1.0
実現したいこと
discord.pyにおいてCogとスラッシュコマンドの両方を同時に使いたいです
発生している問題・エラーメッセージ
python
1# Botのprefixを$<コマンド>として登録しています 2# /hoge とするとエラーメッセージ等無し 3# $hoge とすると 4discord.ext.commands.errors.CommandNotFound: Command "hoge" is not found 5# Cogを読み込んでいないということはありません
該当のソースコード
main.py
内のsetup_hook
の記述
これ以外にapp_commandsにかかわる記述はしていません
python
1async def setup_hook(self) -> None: 2 for extension in DiscordBot.cogs: 3 try: 4 await self.load_extension(extension) 5 except Exception as e: 6 print('Could not load extension {0} due to {1.__class__.__name__}: {1}'.format( 7 extension, e)) 8 await self.tree.sync()
スラッシュコマンド化したいコマンドの入ったCogの記述
python
1import discord 2from discord import app_commands 3from discord.ext import commands 4 5class MyCog(commands.Cog): 6 def __init__(self, bot: commands.Bot) -> None: 7 self.bot = bot 8 9 @app_commands.command() 10 async def hoge(self, interaction: discord.Interaction): 11 """Says hello!""" 12 await interaction.response.send_message(f'Hi, {interaction.user.mention}') 13 14 15async def setup(bot: commands.Bot) -> None: 16 await bot.add_cog(MyCog(bot))
試したこと
main.py
内に直接
python
1@bot.tree.command() 2async def hello(interaction: discord.Interaction): 3 """Says hello!""" 4 await interaction.response.send_message(f'Hi, {interaction.user.mention}')
と記述することで、スラッシュコマンドに登録されていました

回答3件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/12/27 17:50