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

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

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:

import os
import discord
from discord import app_commands

TOKEN = 'BOT TOKEN'
GUILD = 'GUILD NAME'

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client) #For slash command

@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break
    print(f'{client.user} has connected to: \n {guild.name}')

@tree.command(name='apply', description='apply', guild=discord.Object(id=000000))
async def commandno1(interaction: discord.Interaction, name: str, reason: str, exp: str):
    emb = discord.Embed(title='Mod App', description=f'Name: {name} \n Reason: {reason} \n exp:{exp}')
    emb.set_author(name=f'Submitted by {interaction.user}')
    channel = client.fetch_channel(00000000000)  # to send it to the admin for review
    await channel.send(embed=emb)
    await interaction.response.send_message('App submitted', ephemeral=True)

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:

import os
import discord
from discord import app_commands

TOKEN = 'BOT TOKEN'
GUILD = 'GUILD NAME'

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client) #For slash command

@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break
    print(f'{client.user} has connected to: \n {guild.name}')

@tree.command(name='apply', description='apply', guild=discord.Object(id = 000000))
async def commandno1(interaction:discord.Interaction, name:str, reason:str, exp:str):
     emb = discord.Embed(title='Mod App', description=f'Name: {name} \n Reason: {reason} \n exp:{exp}')
     emb.set_author(name='Submitted by {interaction.user}')
     channel = client.fetch_channel(00000000000)  #to send it to the admin for review
     await channel.send(embed=emb)
     await interaction.response.send_message('App submitted', ephemeral=True)

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

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

import discord
from discord.ext import commands

client = commands.Bot()

@client.command(name='apply', description='申请', guild=discord.Object(id=000000))
async def commandno1(interaction: discord.Interaction, name: str, reason: str, exp: str):
    emb = discord.Embed(title='Mod申请', description=f'姓名:{name}\n原因:{reason}\n经验:{exp}')
    emb.set_author(name=f'提交者 {interaction.user}')
    b1 = discord.ui.Button(label='接受', style=discord.ButtonStyle.success)
    b2 = discord.ui.Button(label='拒绝', style=discord.ButtonStyle.danger)
    view = discord.ui.View()
    view.add_item(b1)
    view.add_item(b2)
    channel = await client.fetch_channel(00000000000)  # 发送给管理员进行审核的通道

    async def b1_callback(interaction: discord.Interaction):
        # 添加您的功能

    async def b2_callback(interaction: discord.Interaction):
        # 添加您的功能

    b1.callback = b1_callback
    b2.callback = b2_callback
    await channel.send(embed=emb, view=view)
    await interaction.response.send_message('申请已提交', ephemeral=True)

# 用于记录的消息发送通道
async def log_message(message: str):
    channel = await client.fetch_channel(MY_CHANNEL_ID)
    await channel.send(message)

# 在其他地方调用 log_message 函数来发送消息

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

英文:

It goes like this
Add your button functions under there

import discord
from discord import commands

client = commands.Client()

@tree.command(name='apply', description='apply', guild=discord.Object(id = 000000))
async def commandno1(interaction:discord.Interaction, name:str, reason:str, exp:str):
     emb = discord.Embed(title='Mod App', description=f'Name: {name} \n Reason: {reason} \n exp:{exp}')
     emb.set_author(name='Submitted by {interaction.user}')
     b1 = discord.ui.Button(label='Accept', style=discord.ButtonStyle.success)
     b1 = discord.ui.Button(label='Reject', style=discord.ButtonStyle.danger)
     view = discord.ui.View()
     view.add_item(b1)
     view.add_item(b2)
     channel = client.fetch_channel(00000000000)  #to send it to the admin for review

  
     async def b1_callback(interaction:discord.Interaction):
        #add your function
     async def b2_callback(interaction:discord.Interaction):
        #add your function
        ```

     b1.callback = b1_callback
     b2.callback = b2_callback
     await channel.send(embed=emb, view=view)
     await interaction.response.send_message('App submitted', ephemeral=True)

And for logging use this to send message

channel = client.fetch_channel(MY_CHANNEL_ID)
    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:

确定