我目前正在寻找解决我的问题的方法: 我想知道是否可以让 Discord 机器人运行由同一个 Discord 机器人创建的斜杠命令。
这是我当前的机器人代码:
import discord
from discord import app_commands
class aclient(discord.Client):
def __init__(self):
super().__init__(intents = discord.Intents.all())
self.synced = False #we use this so the bot doesn't sync commands more than once
async def on_ready(self):
await self.wait_until_ready()
if not self.synced: #check if slash commands have been synced
await tree.sync(guild = discord.Object(id=1204091669943816213)) #guild specific: leave blank if global (global registration can take 1-24 hours)
self.synced = True
channel_id = 1204102053064871946 # Channel ID to clear messages
channel = self.get_channel(channel_id)
if channel:
await channel.purge(limit=1) # Delete all messages in the channel
user = client.get_user(int(716235295032344596))
print(f"We have logged in as {self.user}.")
class button_view(discord.ui.View):
def __init__(self) -> None:
super().__init__(timeout=None)
@discord.ui.button(label = "Verify", style = discord.ButtonStyle.green, custom_id = "role_button")
async def verify(self, interaction: discord.Interaction, button: discord.ui.Button):
client.role = interaction.guild.get_role(1204110443258318928)
if client.role not in interaction.user.roles:
await interaction.user.add_roles(client.role)
await interaction.response.send_message(f"I have given you {client.role.mention}!", ephemeral = True)
with open("verified.txt", "w") as file:
file.write(str(interaction.user.id) + "\n")
else: await interaction.response.send_message(f"You already have {client.role.mention}!", ephemeral = True)
client = aclient()
tree = app_commands.CommandTree(client)
@tree.command(guild = discord.Object(id=1204091669943816213), name='rules', description='Rules')
async def rules(interaction: discord.Interaction):
role_id = 1205862534280642640
role = discord.utils.get(interaction.guild.roles, id=role_id)
if role in interaction.user.roles:
embed2=discord.Embed(title="📜 Rules 📜", description='''
**§1 -** __Be respectful__: Treat others with kindness and respect. Harassment, hate speech, or any form of discrimination will not be tolerated.\n
**§2 -** __Keep discussions civil__: Debates and discussions are encouraged, but avoid personal attacks or insults. Disagreements should be handled respectfully.\n
**§3 -** __No spam or self-promotion__: Avoid flooding the chat with unnecessary messages or advertisements.\n
**§4 -** __Use appropriate content__: Keep conversations and content appropriate for all ages. NSFW (Not Safe For Work) content is strictly prohibited.\n
**§5 -** __No trolling__: Do not engage in trolling, flaming, or intentionally disrupting the server environment. This includes excessive use of emojis or CAPS LOCK.\n
**§6 -** __Follow Discord's Terms of Service and Community Guidelines__: Make sure all activities within the server comply with Discord's terms and guidelines.\n
**§7 -** __Respect server staff__: Follow the instructions of moderators and administrators. Disrespect towards server staff will not be tolerated.\n
**§8 -** __Use channels appropriately__: Post content in relevant channels and avoid off-topic discussions. If unsure, ask a staff member for guidance.\n
**§9 -** __Report violations__: If you encounter any violations of the rules or Discord's guidelines, report them to the server staff.
''', color=discord.Colour.blue())
embed2.set_thumbnail(url="https://upload.wikimedia.org/wikipedia/commons/thumb/1/10/Userbox_creeper.svg/800px-Userbox_creeper.svg.png")
await interaction.response.send_message(embed=embed2, view = button_view())
else:
await interaction.response.send_message("You don't have permission to execute this command.", ephemeral=True)
client.run('Token')
在 on_ready 函数中,我希望机器人在
if channel:
之后运行规则斜杠命令。
我想知道是否可以让 Discord 机器人运行由同一个 Discord 机器人创建的 Slash 命令。
没有。
无论是谁的命令,机器人都无法执行。
应用程序命令(斜杠命令)是用户启动的,因此您无法找到任何相关文档。
如果您希望在
if channel
之后再次发送规则嵌入,那么您需要提取创建规则嵌入的代码,并在 if channel
中调用该函数
例如:
def getRulesEmbed():
embed2=discord.Embed(title="📜 Rules 📜", description='''
**§1 -** __Be respectful__: Treat others with kindness and respect. Harassment, hate speech, or any form of discrimination will not be tolerated.\n
**§2 -** __Keep discussions civil__: Debates and discussions are encouraged, but avoid personal attacks or insults. Disagreements should be handled respectfully.\n
**§3 -** __No spam or self-promotion__: Avoid flooding the chat with unnecessary messages or advertisements.\n
**§4 -** __Use appropriate content__: Keep conversations and content appropriate for all ages. NSFW (Not Safe For Work) content is strictly prohibited.\n
**§5 -** __No trolling__: Do not engage in trolling, flaming, or intentionally disrupting the server environment. This includes excessive use of emojis or CAPS LOCK.\n
**§6 -** __Follow Discord's Terms of Service and Community Guidelines__: Make sure all activities within the server comply with Discord's terms and guidelines.\n
**§7 -** __Respect server staff__: Follow the instructions of moderators and administrators. Disrespect towards server staff will not be tolerated.\n
**§8 -** __Use channels appropriately__: Post content in relevant channels and avoid off-topic discussions. If unsure, ask a staff member for guidance.\n
**§9 -** __Report violations__: If you encounter any violations of the rules or Discord's guidelines, report them to the server staff.
''', color=discord.Colour.blue())
embed2.set_thumbnail(url="https://upload.wikimedia.org/wikipedia/commons/thumb/1/10/Userbox_creeper.svg/800px-Userbox_creeper.svg.png")
return embed2
然后你可以获取规则并将其发送到 if 语句中。
rules = getRulesEmbed()
我的主要问题是,在机器人重新启动后,我的交互按钮“验证”不再起作用,而在本指南之后应该如此: https://guide.pycord.dev/interactions/ui-components/buttons
编辑: 我知道机器人已通过按钮发送嵌入,但我仍然有疑问为什么机器人重新启动后按钮不再起作用。