Discord机器人具有签到和签退功能,但顺序不正确。

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

Discord bot with Clock in and clock out functions, but order are not correct

问题

Here's the translated text:

我有一个机器人我希望首先是按钮然后是文本我尝试使用chatgpt来更改顺序但不确定为什么无法让它首先出现

这基本上是一个打卡系统应该首先是文本但我得到的是文本然后是按钮行为有点奇怪

Please note that code elements like variable names, class names, and method names remain unchanged in the translation.

英文:

I have this bot where I want to have Buttons first and text bellow. I tried chatgpt to fix the order but not sure why I can't make it come first

import discord
from discord.ext import commands
from datetime import datetime
import settings

texto1 = []
texto2 = []
bot = discord.Client(intents=discord.Intents.all())
class MyView(discord.ui.View):
    @discord.ui.button(label="Entrar", style=discord.ButtonStyle.green, custom_id="entrar")
    async def entrar_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
        if interaction.user.display_name not in texto1:
            view = MyView()
            texto1.append(interaction.user.display_name)
            texto2.append(f"{interaction.user.display_name} entrou às {datetime.now().strftime('%H:%M:%S')}")
            
            await interaction.message.edit(content=get_text(), view=view)
        await interaction.response.defer()

    @discord.ui.button(label="Sair", style=discord.ButtonStyle.red, custom_id="sair")
    async def sair_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
        if interaction.user.display_name in texto1:
            view = MyView()
            texto1.remove(interaction.user.display_name)
            texto2.append(f"{interaction.user.display_name} saiu às {datetime.now().strftime('%H:%M:%S')}")
            
            await interaction.message.edit(content=get_text(), view=view)
        await interaction.response.defer()


async def update_message(channel):
    # create a MyView object with the buttons
    view = MyView()
    # create an initial message
    text = get_text()
    message = await channel.send(text, view=view)
    # update the message with the actual data
    text = get_text()
    await message.edit(view=view, content=text)




def get_text():
    text1 = '\n'.join(texto1) if texto1 else 'Ninguém'
    text2 = '\n'.join(texto2) if texto2 else 'Nenhum registro ainda'
    text = f"**Quem está trabalhando:**\n{text1}\n\n**Log de chegada e saída:**\n{text2}"
    return text






@bot.event
async def on_ready():
    print('Bot está online')
    channel = bot.get_channel(1105561415038812311) # Insira aqui o ID do canal em que o bot deve ficar
    await update_message(channel)

bot.run(settings.DISCORD_API_SECRET)

Its basically a clock in and out suppose to be text first but I got
text then buttons. kinda weird behave

答案1

得分: 0

"如果您想首先显示按钮,您需要将它们发送到一个单独的消息中,然后再发送另一条带有文本的消息。 - Hazzu

您说得对,我分开发送了:

view = MyView()
# 创建初始消息
message = await channel.send(view=view)

并解决了问题
谢谢"

英文:

> If you want to display buttons first, you'll need to send them in a
> separate message, and then another message with the text. – Hazzu

You were right, I did separate

view = MyView()
    # create an initial message
message = await channel.send(view=view)

and solved the problem
thanks

huangapple
  • 本文由 发表于 2023年5月14日 03:10:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244477.html
匿名

发表评论

匿名网友

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

确定