-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
49 lines (35 loc) · 1.28 KB
/
main.py
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
import asyncio
import os
import discord
from dotenv import load_dotenv
load_dotenv()
butler = discord.Bot()
@butler.event
async def on_ready():
await butler.sync_commands()
print(f'Logged in as {butler.user.name} (ID: {butler.user.id})')
print(f'{len(butler.guilds)} servers connected: {", ".join([guild.name for guild in butler.guilds])}')
@butler.slash_command(name="ping", description="Returns the latency of the bot")
async def ping(ctx):
latency = round(butler.latency * 1000, 2)
await ctx.respond(f'Pong! ({latency}ms)', ephemeral=True)
async def load_extensions():
for filename in os.listdir('extensions'):
if filename.endswith('.py'):
butler.load_extension(f'extensions.{filename[:-3]}')
print(f'Loaded {filename[:-3]} extension')
async def bot_start():
await load_extensions()
await butler.start(os.getenv('BOT_TOKEN'))
def main():
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(bot_start())
except discord.LoginFailure: # Add exception handling for failed login
print("Invalid bot token")
except KeyboardInterrupt: # The bot will keep running until a KeyboardInterrupt
print("Bot stopped.")
finally:
loop.close()
if __name__ == '__main__':
main()