discord.py V2 Cogでサブコマンドを実装する
#main .py #実行するファイル
from discord.ext import commands
from discord import app_commands
import discord
token = ""
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
await bot.load_extension("Cogs.verify")
#下でスラッシュコマンドの登録
await bot.tree.sync()
bot.run(token)
#Cogs /verify.py #ここから別ファイル
import discord
from discord.ext import commands
from discord import app_commands
class Verify(app_commands.Group):
def __init__(self, bot: commands.Bot, **kwargs):
super().__init__(**kwargs)
self.bot = bot
@app_commands.command(name="panel", description="認証用パネルを設置します")
@app_commands.describe(role="認証後に付与する役職を選択してください")
@app_commands.describe(title="パネルのタイトル")
async def verify_panel(self, ctx: discord.Interaction, role: discord.Role, title: str="認証"):
... #処理を書いていく
@app_commands.command(name="delete", description="....")
async def verify_delete(self, ctx: discord.Interaction):
...
async def setup(bot: commands.Bot):
bot.tree.add_command(Verify(bot, name="verify"))
Discord れいちゃん#1111
Twitter @kzv
この記事が気に入ったらサポートをしてみませんか?