Something went wrong. Try again.
My personal Discord Bot programmed in python
Something went wrong. Try again.
Discord_Bot main.py
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138import osimport discord
from discord.ext import commandsfrom discord import app_commandsfrom dotenv import load_dotenv
from lang import load_langs, save_langs, get_user_lang, get_msg
print(discord.__version__)print(discord.__file__)
# Discord Bot Authload_dotenv()DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")intents = discord.Intents.all()intents.message_content = Trueintents.members = Truebot = commands.Bot(command_prefix="!", intents=intents)# The bot later gets activated by the line "bot.run(DISCORD_TOKEN)"
class HelpCommand(commands.HelpCommand): async def send_bot_help(self, mapping): embed = discord.Embed(title="Bot Help") for cog, cmds in mapping.items(): embed.add_field(name=cog.qualified_name, value=f"{len(cmds)} commands") channel = self.get_destination() await channel.send(embed=embed)
@bot.eventasync def on_ready(): try: # If bot can connect to discord, send confirmation on terminal print("Discord bot succesfully connected!") except Exception as e: print(f"[!] couldn't connect, an Error occured! Error: {e}")
try: # Connect to the Cogs, such as "music-commands.py". This way, files become smaller and more manageable. I strongly advice making new cogs instead of just putting everything here! for cog in os.listdir("./cogs"): if cog.endswith("commands.py"): await bot.load_extension(f"cogs.{cog[:-3]}") print("Cogs loaded!") except Exception as e: print(f"Failed to load cogs: {e}")
try: # Sync commands to Discord (for slash commands) synced = await bot.tree.sync() print(f"Synced {len(synced)} slash command(s)") except Exception as e: print(f"Failed to sync commands: {e}")
# Ping command, useful for testing purposes, and learning how to make new commands :3@bot.hybrid_command()async def ping(ctx): """Pings the Bot!""" await ctx.send(f"Pong🏓! | {round(bot.latency * 1000)}ms!") print(f"{ctx.author.name} in {ctx.guild.name} typed '!ping'")
# Voice Channel Commands (Useful for other audio-related commands)@bot.hybrid_command(aliases=["entrar", "j"])async def join(ctx): """Joins the Call""" lang = get_user_lang(ctx.author.id) if ctx.author.voice: channel = ctx.message.author.voice.channel try: await channel.connect() msg = get_msg(lang, "bot_joined_vc").format(channel_name=channel.name) await ctx.send(msg) except Exception as e: await ctx.send(f"Error: {e}") else: msg = get_msg(lang, "user_not_in_vc") await ctx.send(msg) print(f"{ctx.author.name} in {ctx.guild.name} typed '!join'")
@bot.hybrid_command(aliases=["quit", "sair", "q"])async def leave(ctx): """Leaves the Call""" lang = get_user_lang(ctx.author.id) if ctx.author.voice is None: msg = get_msg(lang, "user_not_in_vc") elif ctx.voice_client: channel = ctx.voice_client.channel await ctx.guild.voice_client.disconnect() msg = get_msg(lang, "bot_left_vc").format(channel_name=channel.name) await ctx.send(msg) else: msg = get_msg(lang, "bot_not_in_vc") await ctx.send(msg) print(f"{ctx.author.name} in {ctx.guild.name} typed '!leave'")
# Language Switcher@bot.hybrid_command(aliases=["language", "lingua", "língua", "l"])@app_commands.describe( language="bot language | supported languages: pt (Portuguese) en (English)")async def lang(ctx, language=None): """ Switches Bot Language
Obs: Does NOT switch !help language (if you know how to do this, please dm @snow_floke)
Syntax: !lang <lg>
Supported Languages: 'pt': Portuguese 'en': English """ # Update this line if you add a new language! if language is None: await ctx.send("Please provide a language. Example: '!lang pt'") return elif language not in [ "pt", "en", ]: # To add new languages, just add a new entry to this line! await ctx.send( "Invalid language :( | Available languages: pt, en" ) # Update this one as well!!! return langs = load_langs() langs[str(ctx.author.id)] = language save_langs(langs)
lang = get_user_lang(ctx.author.id) msg = get_msg(lang, "current_language").format(language=lang) await ctx.send(msg) print(f"{ctx.author.name} in {ctx.guild.name} typed '!lang'")
# Bot Innitbot.run(DISCORD_TOKEN)