what should I do if after turning on the bot after 4 minutes, the buttons stop working?
Question:
I’m making a discord bot on python using discordpy. Here are the imports:
import discord
from random import randint
import json
from discord.ext import commands
from discord.User interface import button, view
Here is an example of a button:
class ButtonSandStone8(Button):
def __init__(self,label):
super().__init__(label=label,style=discord.ButtonStyle.grey,custom_id='SandStone_btn8')
async def callback(self, interaction):
global queue8
if "<@" + str(interaction.user.id) + ">" + "n"+ "n"+ "n" in queue8:
global lobby_voice8
global ct8
global t8
global voice_ct8
global voice_t8
voice_state = interaction.user.voice
if voice_state is None:
embed = discord.Embed(description='Вы должны зайти в войс, чтобы участвовать!',color=discord.Color.dark_grey())
return await interaction.response.send_message(embed=embed, ephemeral=True)
elif interaction.user.voice.channel.id == lobby_voice8:
global voices8
if len(queue8)-10 < 10:
embed = discord.Embed(description='Очередь не полная!',color=discord.Color.dark_grey())
await interaction.response.send_message(embed=embed, ephemeral=True)
elif len(queue8)-10 == 10 and interaction.user not in voices8 and "<@" + str(interaction.user.id) + ">" + "n"+ "n"+ "n" in queue8:
voices8.append(interaction.user)
global SandStone8
global Rust8
global Zone98
global Province8
global Breeze8
SandStone8 += 1
if len(voices8) < 10:
embed = discord.Embed(description='+1 голос за карту SandStone', color=discord.Color.dark_grey())
await interaction.response.send_message(embed=embed, ephemeral=True)
if len(voices8) == 10:
global msg8
await msg8.delete()
if SandStone8 > Rust8 and SandStone8 > Zone98 and SandStone8 > Breeze8 and SandStone8 > Province8:
await distribution_sandstone8(interaction)
elif Rust8 > SandStone8 and Rust8 > Zone98 and Rust8 > Breeze8 and Rust8 > Province8:
await distribution_rust8(interaction)
elif Zone98 > SandStone8 and Zone98 > Rust8 and Zone98 > Province8 and Zone98 > Breeze8:
await distribution_zone98(interaction)
elif Province8 > SandStone8 and Province8 > Rust8 and Province8 > Zone98 and Province8 > Breeze8:
await distribution_province8(interaction)
elif Breeze8 > SandStone8 and Breeze8 > Rust8 and Breeze8 > Zone98 and Breeze8 > Province8:
await distribution_breeze8(interaction)
else:
voices8.clear()
SandStone8 = 0
Rust8 = 0
Zone98 = 0
Province8 = 0
Breeze8 = 0
embed = discord.Embed(description='Ничья! Голосование повторяется', color=discord.Color.dark_grey())
await interaction.response.send_message(embed=embed, delete_after=180)
else:
embed = discord.Embed(description='Вы подключены не к тому голосовому каналу!',color=discord.Color.dark_grey())
await interaction.response.send_message(embed=embed, ephemeral=True)
else:
embed = discord.Embed(description='Вас нет в очереди чтобы голосовать!',color=discord.Color.red())
await interaction.response.send_message(embed=embed, ephemeral=True)
Here is an example of a button call:
SandStone_btn8 = ButtonSandStone8("1.SandStone")
w = View()
w.add_item(SandStone_btn8)
My idea is to somehow restart the bot every 3 minutes, but maybe there is another way to fix it
Answers:
The default value for the timeout
kwarg to View
is 180, meaning the view times out after 180 seconds. If you want a different timeout, pass a value for it. A value of None
means it doesn’t time out.
View(timeout=None)
The timeout in seconds from last interaction with the UI before no longer accepting input. If None then there is no timeout.
However, this is only as long as your bot is running, and all components will stop working when you restart your bot. If you want to avoid this, there’s an official example on how to make persistent views. https://github.com/Rapptz/discord.py/blob/master/examples/views/persistent.py
I’m making a discord bot on python using discordpy. Here are the imports:
import discord
from random import randint
import json
from discord.ext import commands
from discord.User interface import button, view
Here is an example of a button:
class ButtonSandStone8(Button):
def __init__(self,label):
super().__init__(label=label,style=discord.ButtonStyle.grey,custom_id='SandStone_btn8')
async def callback(self, interaction):
global queue8
if "<@" + str(interaction.user.id) + ">" + "n"+ "n"+ "n" in queue8:
global lobby_voice8
global ct8
global t8
global voice_ct8
global voice_t8
voice_state = interaction.user.voice
if voice_state is None:
embed = discord.Embed(description='Вы должны зайти в войс, чтобы участвовать!',color=discord.Color.dark_grey())
return await interaction.response.send_message(embed=embed, ephemeral=True)
elif interaction.user.voice.channel.id == lobby_voice8:
global voices8
if len(queue8)-10 < 10:
embed = discord.Embed(description='Очередь не полная!',color=discord.Color.dark_grey())
await interaction.response.send_message(embed=embed, ephemeral=True)
elif len(queue8)-10 == 10 and interaction.user not in voices8 and "<@" + str(interaction.user.id) + ">" + "n"+ "n"+ "n" in queue8:
voices8.append(interaction.user)
global SandStone8
global Rust8
global Zone98
global Province8
global Breeze8
SandStone8 += 1
if len(voices8) < 10:
embed = discord.Embed(description='+1 голос за карту SandStone', color=discord.Color.dark_grey())
await interaction.response.send_message(embed=embed, ephemeral=True)
if len(voices8) == 10:
global msg8
await msg8.delete()
if SandStone8 > Rust8 and SandStone8 > Zone98 and SandStone8 > Breeze8 and SandStone8 > Province8:
await distribution_sandstone8(interaction)
elif Rust8 > SandStone8 and Rust8 > Zone98 and Rust8 > Breeze8 and Rust8 > Province8:
await distribution_rust8(interaction)
elif Zone98 > SandStone8 and Zone98 > Rust8 and Zone98 > Province8 and Zone98 > Breeze8:
await distribution_zone98(interaction)
elif Province8 > SandStone8 and Province8 > Rust8 and Province8 > Zone98 and Province8 > Breeze8:
await distribution_province8(interaction)
elif Breeze8 > SandStone8 and Breeze8 > Rust8 and Breeze8 > Zone98 and Breeze8 > Province8:
await distribution_breeze8(interaction)
else:
voices8.clear()
SandStone8 = 0
Rust8 = 0
Zone98 = 0
Province8 = 0
Breeze8 = 0
embed = discord.Embed(description='Ничья! Голосование повторяется', color=discord.Color.dark_grey())
await interaction.response.send_message(embed=embed, delete_after=180)
else:
embed = discord.Embed(description='Вы подключены не к тому голосовому каналу!',color=discord.Color.dark_grey())
await interaction.response.send_message(embed=embed, ephemeral=True)
else:
embed = discord.Embed(description='Вас нет в очереди чтобы голосовать!',color=discord.Color.red())
await interaction.response.send_message(embed=embed, ephemeral=True)
Here is an example of a button call:
SandStone_btn8 = ButtonSandStone8("1.SandStone")
w = View()
w.add_item(SandStone_btn8)
My idea is to somehow restart the bot every 3 minutes, but maybe there is another way to fix it
The default value for the timeout
kwarg to View
is 180, meaning the view times out after 180 seconds. If you want a different timeout, pass a value for it. A value of None
means it doesn’t time out.
View(timeout=None)
The timeout in seconds from last interaction with the UI before no longer accepting input. If None then there is no timeout.
However, this is only as long as your bot is running, and all components will stop working when you restart your bot. If you want to avoid this, there’s an official example on how to make persistent views. https://github.com/Rapptz/discord.py/blob/master/examples/views/persistent.py