44from datetime import datetime , timedelta
55import operator
66import os
7+ import json
78from dotenv import load_dotenv
89import sqlite3
910
1415intents .voice_states = True
1516intents .members = True
1617
17- client = commands .Bot (command_prefix = '!' , intents = intents )
18+ client = commands .Bot (command_prefix = None , intents = intents )
1819slash = SlashCommand (client )
1920
21+ # Load locales
22+ def load_locale (lang = None ):
23+ if not lang :
24+ lang = os .getenv ('BOT_LOCALE' , 'en_US' )
25+
26+ try :
27+ with open (f'locales/{ lang } .json' , 'r' , encoding = 'utf-8' ) as f :
28+ return json .load (f )
29+ except FileNotFoundError :
30+ # Fallback to English if locale file not found
31+ with open ('locales/en_US.json' , 'r' , encoding = 'utf-8' ) as f :
32+ return json .load (f )
33+
34+ # Load locale from environment variable
35+ locale = load_locale ()
36+
2037# Connect to SQLite database
2138conn = sqlite3 .connect ('bot_database.db' )
2239cursor = conn .cursor ()
3047 last_call_time TIMESTAMP
3148 )
3249''' )
50+
3351conn .commit ()
3452
3553# Dictionary of messages per channel
3856 channel_env = f'CHANNEL_{ i } '
3957 channel_id = os .getenv (channel_env )
4058 if channel_id :
41- channel_messages [channel_id ] = " {member.name} is in {channel_name}"
59+ channel_messages [channel_id ] = locale [ "user_in_channel" ]. format ( member = " {member.name}" , channel = " {channel_name}")
4260 else :
4361 break # Stop the loop if there are no more defined channels
4462
@@ -74,10 +92,19 @@ async def on_voice_state_update(member, before, after):
7492 if before .channel == after .channel :
7593 return # Ignore updates that don't involve channel changes
7694
95+ notification_channel_id = os .getenv ('NOTIFICATION_CHANNEL' )
96+ if not notification_channel_id :
97+ return
98+
99+ channel = client .get_channel (int (notification_channel_id ))
100+ if not channel :
101+ return
102+
103+ # User joined a voice channel
77104 if after .channel :
78105 if len (after .channel .members ) == 1 :
79- channel = client . get_channel ( int ( os . getenv ( 'NOTIFICATION_CHANNEL' )))
80- await channel .send (f" { member . name } is in { after . channel . name } " )
106+ # First person in the channel - mark with @everyone
107+ await channel .send (locale [ "user_joined_call" ]. format ( member = member . mention ) )
81108
82109 # Update entry count and last call time in the database
83110 cursor .execute ('''
@@ -89,26 +116,31 @@ async def on_voice_state_update(member, before, after):
89116 if show_log :
90117 print (f"{ member .name } entered a channel." )
91118 else :
92- cursor .execute ('SELECT last_call_time FROM users WHERE id = ?' , (member .id ,))
93- result = cursor .fetchone ()
94- last_call_time = datetime .fromisoformat (result [0 ]) if result else datetime .min
95- if datetime .now () - last_call_time >= timedelta (minutes = 60 ):
96- cursor .execute ('UPDATE users SET last_call_time = ? WHERE id = ?' , (datetime .now (), member .id ))
97- conn .commit ()
98- channel = client .get_channel (1206713130353295450 )
99- if not before .channel :
100- await channel .send (f"{ member .mention } is in the call! @here" )
119+ # Other people joining - just show channel info
120+ await channel .send (locale ["user_in_channel" ].format (member = member .name , channel = after .channel .name ))
121+
122+ # User left a voice channel
123+ if before .channel and not after .channel :
124+ await channel .send (locale ["user_left_channel" ].format (member = member .name , channel = before .channel .name ))
101125
102- if before .channel and after .channel and before .channel != after .channel and show_log :
103- print (f"{ member .name } was moved from { before .channel .name } to { after .channel .name } " )
126+ # User moved between channels
127+ if before .channel and after .channel and before .channel != after .channel :
128+ await channel .send (locale ["user_moved_to_channel" ].format (
129+ member = member .name ,
130+ old_channel = before .channel .name ,
131+ new_channel = after .channel .name
132+ ))
133+
134+ if show_log :
135+ print (f"{ member .name } was moved from { before .channel .name } to { after .channel .name } " )
104136
105137@slash .slash (name = "leaders" , description = "Shows how many times each user entered calls." )
106138async def leaders (ctx ):
107139 cursor .execute ('SELECT id, name, entry_count FROM users ORDER BY entry_count DESC LIMIT 10' )
108140 leaderboard = cursor .fetchall ()
109- leaderboard_text = "Entry Leaderboard: \n "
141+ leaderboard_text = locale [ "leaderboard_title" ] + " \n "
110142 for idx , (user_id , name , count ) in enumerate (leaderboard , start = 1 ):
111- leaderboard_text += f" { idx } . { name } : { count } times \n "
143+ leaderboard_text += locale [ "leaderboard_entry" ]. format ( position = idx , name = name , count = count ) + " \n "
112144 await ctx .send (content = leaderboard_text )
113145 if show_log :
114146 print ('Leaderboard command executed.' )
@@ -117,18 +149,18 @@ async def leaders(ctx):
117149async def toggle_message (ctx ):
118150 global send_message_enabled
119151 send_message_enabled = not send_message_enabled
120- status = "enabled" if send_message_enabled else "disabled"
121- await ctx .send (content = f"Functionality to send a message when someone enters a call is now { status } ." )
152+ status = locale [ "toggle_enabled" ] if send_message_enabled else locale [ "toggle_disabled" ]
153+ await ctx .send (content = status )
122154
123155@slash .slash (name = "help" , description = "Get a list of commands." )
124156async def help (ctx ):
125157 commands = [
126- "/leaders - Shows how many times each user entered calls." ,
127- "/toggle - Turns on/off the functionality of sending a message when someone enters a call." ,
128- "/help - Get a list of commands."
158+ locale [ "help_leaders" ] ,
159+ locale [ "help_toggle" ] ,
160+ locale [ "help_help" ]
129161 ]
130162 command_list = "\n " .join (commands )
131- await ctx .send (content = f"Hello, { ctx .author .name } ! Here's the list of available commands: \n \n { command_list } " )
163+ await ctx .send (content = locale [ "help_title" ]. format ( author = ctx .author .name ) + " \n \n " + command_list )
132164
133165# Load the bot token from environment variables
134166TOKEN = os .getenv ('DISCORD_BOT_TOKEN' )
0 commit comments