forked from MicrosoftStudentChapter/makeathon6-discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot.py
53 lines (45 loc) · 1.54 KB
/
bot.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
50
51
52
53
import discord
from discord.ext import commands, tasks
from itertools import cycle
import os
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
token = os.getenv("DISCORD_TOKEN")
# Intents setup
intents = discord.Intents.all()
intents.members = True
intents.presences = True
intents.messages = True
# Bot setup with command prefix and intents
client = commands.Bot(command_prefix="!", intents=intents)
client.remove_command('help')
bot_status = cycle(["Status One", "Status Two", "Status Three"])
# Background task to change bot status
@tasks.loop(seconds=5)
async def change_status():
await client.change_presence(activity=discord.Game(next(bot_status)))
# Event when bot is ready
@client.event
async def on_ready():
print("Bot is connected to Discord")
change_status.start()
# Function to load all cogs
cogs_path = "tech-week-discord-bot\cogs"
async def load_cogs():
for filename in os.listdir(cogs_path):
if (filename.endswith(".py") and filename[:-3] != "connection"):
try:
await client.load_extension(f"cogs.{filename[:-3]}")
print(f"Loaded extension: {filename}")
except Exception as e:
print(f"Failed to load extension {filename}: {type(e).__name__} - {e}")
# Main coroutine to start the bot
async def main():
async with client:
await load_cogs()
await client.start(token) # Retrieve token from environment variable
# Run the main coroutine
if __name__ == "__main__":
asyncio.run(main())