如何在discord.py的提交机器人中添加接受/拒绝按钮?

huangapple go评论106阅读模式
英文:

How to add Accept/Deny button to a submission bot discord.py?

问题

I am trying to make a discord applications bot for members to apply for moderator. However, I am not able to add the Accept/Reject buttons to the embed. Here is the bot source code:

  1. import os
  2. import discord
  3. from discord import app_commands
  4. TOKEN = 'BOT TOKEN'
  5. GUILD = 'GUILD NAME'
  6. intents = discord.Intents.default()
  7. intents.message_content = True
  8. client = discord.Client(intents=intents)
  9. tree = app_commands.CommandTree(client) #For slash command
  10. @client.event
  11. async def on_ready():
  12. for guild in client.guilds:
  13. if guild.name == GUILD:
  14. break
  15. print(f'{client.user} has connected to: \n {guild.name}')
  16. @tree.command(name='apply', description='apply', guild=discord.Object(id=000000))
  17. async def commandno1(interaction: discord.Interaction, name: str, reason: str, exp: str):
  18. emb = discord.Embed(title='Mod App', description=f'Name: {name} \n Reason: {reason} \n exp:{exp}')
  19. emb.set_author(name=f'Submitted by {interaction.user}')
  20. channel = client.fetch_channel(00000000000) # to send it to the admin for review
  21. await channel.send(embed=emb)
  22. await interaction.response.send_message('App submitted', ephemeral=True)
  23. client.run(TOKEN)

I was trying to get 2 buttons below the embed. One to accept, other to deny. Please let me know how to add those buttons.

( It should be those clickable buttons, not reactions )

Also, once a button is clicked, I need to log it to a channel, so let me know how to do that as well. Thank you.

  • Python 3.11 IDLE
  • discord.client()
  • app_commands.commandTree()
  • discord.py 2.2.3
英文:

I am trying to make a discord applications bot for members to apply for moderator. However, I am not able to add the Accept/Reject buttons to the embed. Here is the bot source code:

  1. import os
  2. import discord
  3. from discord import app_commands
  4. TOKEN = 'BOT TOKEN'
  5. GUILD = 'GUILD NAME'
  6. intents = discord.Intents.default()
  7. intents.message_content = True
  8. client = discord.Client(intents=intents)
  9. tree = app_commands.CommandTree(client) #For slash command
  10. @client.event
  11. async def on_ready():
  12. for guild in client.guilds:
  13. if guild.name == GUILD:
  14. break
  15. print(f'{client.user} has connected to: \n {guild.name}')
  16. @tree.command(name='apply', description='apply', guild=discord.Object(id = 000000))
  17. async def commandno1(interaction:discord.Interaction, name:str, reason:str, exp:str):
  18. emb = discord.Embed(title='Mod App', description=f'Name: {name} \n Reason: {reason} \n exp:{exp}')
  19. emb.set_author(name='Submitted by {interaction.user}')
  20. channel = client.fetch_channel(00000000000) #to send it to the admin for review
  21. await channel.send(embed=emb)
  22. await interaction.response.send_message('App submitted', ephemeral=True)
  23. client.run(TOKEN)

I was trying to get 2 buttons below the embed. One to accept, other to deny.
Please let me know how to add those buttons.

( It should be those clickable buttons, not reactions )

Also, once a button is clicked, I need to log it to a channel, so let me know how to do that as well. Thank you.

  • Python 3.11 IDLE
  • discord.client()
  • app_commands.commandTree()
  • discord.py 2.2.3

答案1

得分: 1

这是您的代码的中文翻译:

  1. import discord
  2. from discord.ext import commands
  3. client = commands.Bot()
  4. @client.command(name='apply', description='申请', guild=discord.Object(id=000000))
  5. async def commandno1(interaction: discord.Interaction, name: str, reason: str, exp: str):
  6. emb = discord.Embed(title='Mod申请', description=f'姓名:{name}\n原因:{reason}\n经验:{exp}')
  7. emb.set_author(name=f'提交者 {interaction.user}')
  8. b1 = discord.ui.Button(label='接受', style=discord.ButtonStyle.success)
  9. b2 = discord.ui.Button(label='拒绝', style=discord.ButtonStyle.danger)
  10. view = discord.ui.View()
  11. view.add_item(b1)
  12. view.add_item(b2)
  13. channel = await client.fetch_channel(00000000000) # 发送给管理员进行审核的通道
  14. async def b1_callback(interaction: discord.Interaction):
  15. # 添加您的功能
  16. async def b2_callback(interaction: discord.Interaction):
  17. # 添加您的功能
  18. b1.callback = b1_callback
  19. b2.callback = b2_callback
  20. await channel.send(embed=emb, view=view)
  21. await interaction.response.send_message('申请已提交', ephemeral=True)
  22. # 用于记录的消息发送通道
  23. async def log_message(message: str):
  24. channel = await client.fetch_channel(MY_CHANNEL_ID)
  25. await channel.send(message)
  26. # 在其他地方调用 log_message 函数来发送消息

请注意,我已经修复了一些代码错误,并将 discord.Client() 更改为 discord.ext.commands.Bot() 以正确扩展 commands。另外,您需要在 log_message 函数中提供您的通道ID。

英文:

It goes like this
Add your button functions under there

  1. import discord
  2. from discord import commands
  3. client = commands.Client()
  4. @tree.command(name='apply', description='apply', guild=discord.Object(id = 000000))
  5. async def commandno1(interaction:discord.Interaction, name:str, reason:str, exp:str):
  6. emb = discord.Embed(title='Mod App', description=f'Name: {name} \n Reason: {reason} \n exp:{exp}')
  7. emb.set_author(name='Submitted by {interaction.user}')
  8. b1 = discord.ui.Button(label='Accept', style=discord.ButtonStyle.success)
  9. b1 = discord.ui.Button(label='Reject', style=discord.ButtonStyle.danger)
  10. view = discord.ui.View()
  11. view.add_item(b1)
  12. view.add_item(b2)
  13. channel = client.fetch_channel(00000000000) #to send it to the admin for review
  14. async def b1_callback(interaction:discord.Interaction):
  15. #add your function
  16. async def b2_callback(interaction:discord.Interaction):
  17. #add your function
  18. ```
  19. b1.callback = b1_callback
  20. b2.callback = b2_callback
  21. await channel.send(embed=emb, view=view)
  22. await interaction.response.send_message('App submitted', ephemeral=True)

And for logging use this to send message

  1. channel = client.fetch_channel(MY_CHANNEL_ID)
  2. await channel.send(f"message")

huangapple
  • 本文由 发表于 2023年5月21日 18:21:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定