如何在 DM 中隐藏分组斜线命令? 我在下面提供了一些 python 代码示例,其中包含普通斜杠命令 (
bot.tree
) 和分组斜杠命令(类 TestGroup
)。 @discord.app_commands.guild_only()
与 hidden-test
命令完美配合,但不适用于其他命令,我已经尝试了多种方法来解决这种情况,但似乎没有什么对我有用。
import discord
from discord.ext import commands
# Setup
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="!", intents=intents)
# Simple login confirmation
@bot.event
async def on_ready():
await bot.tree.sync()
print(f"{bot.user} is online!")
# @discord.app_commands.guild_only() hides the command if the user try's to use it in DMs.
@bot.tree.command(name="hidden-test", description="This command can only be used in guilds!")
@discord.app_commands.guild_only()
async def hidden_command(interaction: discord.Interaction):
await interaction.response.send_message("This command can only be used in guilds!", ephemeral=True)
# This is a discord slash command group, I tried testing "@discord.app_commands.guild_only()" here, it doesn't work.
class TestGroup(discord.app_commands.Group):
def __init__(self):
super().__init__(name="hidden", description="These command can only be used in guilds!")
@discord.app_commands.command(name="true", description="This command can only be used in guilds!")
@discord.app_commands.guild_only()
async def true_command(self, interaction: discord.Interaction):
await interaction.response.send_message("This command can only be used in guilds! (not really :d)", ephemeral=True)
bot.tree.add_command(TestGroup())
# Replace with your bot token, if you're going to test it.
bot.run("YOUR-TOKEN")
尝试使用
app_commands
装饰器还有更多东西。
结果:斜杠命令显示在我的 DM 中与机器人一起。
预期结果:斜杠命令没有出现在我的机器人私信中,就像“隐藏测试”命令一样。
您在使用子类方法创建的组上使用了装饰器
@discord.app_commands.guild_only()
,但它仅适用于单个命令组或GroupCog方法,我已在GroupCog方法上实现了它:
import discord
from discord.ext import commands
.
.
.
# Setup
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="!", intents=intents)
# Simple login confirmation
@bot.event
async def on_ready():
await bot.tree.sync()
print(f"{bot.user} is online!")
# @discord.app_commands.guild_only() hides the command if the user tries to use it in DMs.
@bot.tree.command(name="hidden-test", description="This command can only be used in guilds!")
@discord.app_commands.guild_only()
async def hidden_command(interaction: discord.Interaction):
await interaction.response.send_message("This command can only be used in guilds!", ephemeral=True)
### Changes
# GroupCog for organizing commands in a cog
@discord.app_commands.guild_only()
class HiddenCommandsCog(commands.GroupCog, name="hidden"):
"""These commands can only be used in guilds!"""
def __init__(self, bot: commands.Bot):
self.bot = bot
super().__init__()
@discord.app_commands.command(name="true", description="This command can only be used in guilds!")
@discord.app_commands.guild_only()
async def true_command(self, interaction: discord.Interaction):
await interaction.response.send_message("This command can only be used in guilds! (not really :d)", ephemeral=True)
bot.add_cog(HiddenCommandsCog(bot))
# Replace with your bot token, if you're going to test it.
bot.run("YOUR-TOKEN")
https://fallendeity.github.io/discord.py-masterclass/slash-commands/#app_commandsguild_only https://fallendeity.github.io/discord.py-masterclass/slash-commands/#__tabbed_8_3