LoginSignup
2
2

More than 1 year has passed since last update.

discord.pyでスラッシュコマンドをサーバー限定にする

Posted at

discord.pyでスラッシュコマンドをサーバー限定にする

意外と躓いたのでメモ
2.0以降discordモジュールはいろんなのでてるけど
今年更新決定したRapptz/discord.pyを引き続き使っていくことにします。

実際のコード

import discord
from discord import app_commands,ui

client = discord.Client(intents=discord.Intents.all())
tree = app_commands.CommandTree(client)

@client.event
async def on_ready():
    #syncはあんまりon_ready()よくないらしいけどとりあえず。
    await tree.sync(guild=discord.Object(id={サーバーID}))

@tree.command(name="test",description="テストです")
@discord.app_commands.guilds({サーバーID})
async def test_command(interaction: discord.Interaction):
    #なんか処理

client.run({discord トークン})

最初以下の2つをどちらかしか入れていなくてうまく機能しなかった。。。

await tree.sync(guild=discord.Object(id={サーバーID}))
@discord.app_commands.guilds({サーバーID})

当然ギルドコマンドになるためDMとかでは表示もされないけど
DMを使えなくするには以下の方法もあるらしい

@discord.app_commands.guild_only()
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2