I'm having issues with this bot, even if runs without errors it still not changing the message, I gave the bot all privildges and still failing to edit the message
import discord
from discord.ext import commands
import settings
from datetime import datetime
class SimpleView(discord.ui.View):
def __init__(self):
super().__init__()
self.foo = None
self.trabalhando = []
self.entrar_button = discord.ui.Button(label="Entrar", style=discord.ButtonStyle.green, custom_id="entrar_button")
self.add_item(self.entrar_button)
self.sair_button = discord.ui.Button(label="Sair", style=discord.ButtonStyle.red, custom_id="sair_button")
self.add_item(self.sair_button)
async def update_message(self, interaction: discord.Interaction):
await interaction.message.edit(content=f"```Mecânicos trabalhando: \n" + "\n".join([member.display_name for member in self.trabalhando]) + "```")
async def enter_button_callback(self, interaction: discord.Interaction):
member = interaction.user
current_time = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
self.trabalhando.append(member)
await self.update_message(interaction)
async def exit_button_callback(self, interaction: discord.Interaction):
member = interaction.user
if member in self.trabalhando:
self.trabalhando.remove(member)
await self.update_message(interaction)
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
@bot.event
async def on_ready():
guild = bot.get_guild(settings.GUILD_ID_INT)
view = SimpleView()
message = await guild.get_channel(settings.FEEDBACK_CH).send(view=view)
await guild.get_channel(settings.FEEDBACK_CH).send("```Mecânicos trabalhando:```")
@bot.event
async def on_button_click(interaction: discord.Interaction):
if interaction.custom_id.startswith('entrar_button'):
view = SimpleView()
await view.enter_button_callback(interaction)
elif interaction.custom_id.startswith('sair_button'):
view = SimpleView()
await view.exit_button_callback(interaction)
else:
await interaction.response.send_message("Unknown button clicked.", ephemeral=True)
bot.run(settings.DISCORD_API_SECRET, root_logger=True)
ok so it suppose to as it runs the bot aways have this 2 buttons Log in and Log out and a message below with the list of users and time they loged in and time they loged out The way shows on discord my plan was to make the buttons and the list who is check in, if you check out you should be removed from the list and after that have a log message who checked and in and out and time 20 last actions
I didn't find any reference to the on_button_click
event in the discord.py
documentation. Maybe you are trying to use on_interaction:
view = SimpleView()
@bot.event
async def on_ready():
guild = bot.get_guild(settings.GUILD_ID_INT)
message = await guild.get_channel(settings.FEEDBACK_CH).send(view=view)
await guild.get_channel(settings.FEEDBACK_CH).send("```Mecânicos trabalhando:```")
@bot.event
async def on_interaction(interaction: discord.Interaction):
item_id = interaction.data.get("custom_id")
if item_id is None:
return
if item_id == "entrar_button":
await view.enter_button_callback(interaction)
elif item_id == "sair_button":
await view.exit_button_callback(interaction)
- In order to be able to access and modify the attributes and methods of your
view
object, you cannot override it elsewhere in the code. Use the object as a global variable;- Do not use an
else
statement foritem_id
, as theon_interaction
event will be called for all interactions of your bot. It could be that you are using buttons with views in another part of your bot and they end up being responded to in this event because ofelse
;- It looks like you are trying to develop a persistent view. See this example on how to work with persistent views in
discord.py
.