Repository navigation
Expand file tree
/
Copy pathbootstrap.py
More file actions
132 lines (93 loc) · 3.87 KB
/
Copy pathbootstrap.py
File metadata and controls
132 lines (93 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import discord
import os
import subprocess
import importlib
import logging
from discord.ext import commands
import botconfig as config
import projectconfig as pconf
from library.core import console
from library.music.instances import get_player
subprocess.run(args="cls" if os.name == "nt" else "clear", shell=True)
D_Intents = discord.Intents.all()
bot = commands.Bot(
command_prefix=config.CommandPrefix,
intents=D_Intents
)
D_Player_Logger = logging.getLogger("discord.player")
D_Player_Logger.setLevel(logging.WARNING)
def load_commands(bot):
commands_dir = os.path.join(os.path.dirname(__file__), "commands")
for root, dirs, files in os.walk(commands_dir):
for file in files:
if file.endswith(".py") and file != "__init__.py":
rel_dir = os.path.relpath(root, os.path.dirname(__file__))
module_path = (
rel_dir.replace(os.sep, ".") + "." + file[:-3]
)
try:
module = importlib.import_module(module_path)
if hasattr(module, "setup"):
module.setup(bot)
except Exception as e:
console.warn(f"Something Was Wrong While Loading Commands From {str(module_path)} As: {str(e)}")
console.info("All Commands Modules Was Loaded Successfully!")
load_commands(bot)
@bot.command(
name="sync",
help="Sync Slash Commands For Your Server! (Admin Only)"
)
@commands.has_permissions(administrator=True)
async def sync(ctx):
try:
await bot.tree.sync(guild=ctx.guild)
await ctx.send("Synced Slash Commands To This Server!")
console.log(f"Synced Slash Commands To Server: {ctx.guild.name} ({ctx.guild.id})")
except Exception as e:
await ctx.send("Error While Syncing Commands")
console.error(f"Error While Sync Commands As: {e}")
@bot.event
async def on_voice_state_update(member, before, after):
if before.channel is not None:
voice_channel = before.channel
player = get_player(voice_channel.guild, bot)
if player and player.voice and player.voice.channel == voice_channel:
non_bot_members = [
m for m in voice_channel.members if not m.bot or m.id == bot.user.id
]
if len(non_bot_members) == 1 and non_bot_members[0].id == bot.user.id:
console.log("Leaving Bot Due No Members In Voice Channel Or Bot Got Kicked!")
try:
await player.disconnect()
except Exception as e:
console.warn(f"Something Went Wrong While Disconnecting As: {e}")
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
await ctx.send(f"Bro What? 🤨 I Think I Don't Know Command Named `{ctx.message.content}`!")
return
elif isinstance(error, commands.errors.MissingPermissions):
await ctx.send(f"Hey! You're Missing Some Permissions To Use That Command!")
return
raise error
@bot.event
async def on_ready():
console.log(f"Bot Installed In: {len(bot.guilds)} Server{'s' if len(bot.guilds) != 1 else ''}")
console.log(f"App Version: {pconf.VersionId}")
console.info(f"Bot Is Right Now Online As: {bot.user.name} ({bot.user}) (Bot User Id: {bot.user.id})")
await bot.change_presence(
activity=discord.CustomActivity(
name=f"{str(config.status)}"
),
status=discord.Status.online
)
try:
synced = await bot.tree.sync()
console.info(f"We're Now Sync All {len(synced)} Commands Successfully!")
except Exception as e:
console.warn(f"Some Exception While Syncing Commands As: {e}")
console.warn("New Command Will Not Added Until Next StartUp Or Manual Sync!")
bot.run(
str(config.Discord_Bot_Token),
reconnect=True
)