import discord
import json
from discord.ui import View, Select
bot = discord.Bot()
with open('config.json', 'r') as f:
config = json.load(f)
token = config['token']
admin_id = config['admin_id']
channel_id = config['channel_id']
message_id = config.get('message_id', None)
class LanguageView(View):
def __init__(self):
super().__init__()
self.add_item(LanguageSelect())
class LanguageSelect(Select):
def __init__(self):
options = [
discord.SelectOption(label="English", value="en", emoji="????????"),
discord.SelectOption(label="Lithuanian", value="lt", emoji="????????"),
discord.SelectOption(label="Russian", value="ru", emoji="????????")
]
super().__init__(placeholder="Select a language", options=options)
async def callback(self, interaction: discord.Interaction):
selected_value = self.values[0]
if selected_value == "en":
await interaction.response.send_message("You selected English.", ephemeral=True, delete_after=5)
elif selected_value == "lt":
await interaction.response.send_message("You selected Lithuanian.", ephemeral=True, delete_after=5)
elif selected_value == "ru":
await interaction.response.send_message("You selected Russian.", ephemeral=True, delete_after=5)
@bot.event
async def on_ready():
print(f"{bot.user} is ready and online!")
if message_id:
try:
channel = bot.get_channel(channel_id)
if channel is not None:
message = await channel.fetch_message(message_id)
view = LanguageView()
await message.edit(view=view)
else:
print(f"Channel with ID {channel_id} not found.")
except discord.NotFound:
print("Message not found.")
@bot.slash_command(name="hello", description="Say hello to the bot")
async def hello(ctx: discord.ApplicationContext):
await ctx.respond("Hey!")
@bot.slash_command(name="send_language")
async def send_language_selection(ctx: discord.ApplicationContext):
if ctx.user.id == admin_id:
await ctx.respond(f"Success", delete_after=5)
message = await ctx.send(f"Select a language")
with open('config.json', 'w') as f:
config['message_id'] = message.id
config['channel_id'] = message.channel.id # Save channel ID
json.dump(config, f, indent=4)
view = LanguageView()
await message.edit(view=view)
else:
await ctx.respond(f"You don't have access to it", delete_after=5)
bot.run(token)
When the bot restarts, the select menu reverts to its initial state, displaying the placeholder instead of the user’s previous selection. As a result, users cannot see what they previously selected, causing confusion and inconvenience.
My attempts to resolve the issue by searching on Google and consulting chatbots like chat-gpt and Claude.ai yielded no results.
New contributor
aurum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.