-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
125 lines (97 loc) · 3.99 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
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
import sys
sys.dont_write_bytecode = True
import asyncio
import os
import platform
from datetime import datetime
from urllib.parse import quote
import discord
from colorama import Fore
from discord.ext import commands, tasks
from helpers.config import check_config, get_config_value
from helpers.general import (clear_console, print_failure_message,
print_success_message)
check_config()
token = get_config_value("token")
greet = get_config_value("greetmembers")
loaded = 0
allcogs = 0
class Bot(commands.Bot):
def __init__(self, *, intents: discord.Intents):
super().__init__(command_prefix=commands.when_mentioned_or("$$"), intents=intents)
async def setup_hook(self):
global loaded, allcogs
clear_console()
for filepath in os.listdir('cogs'):
for filename in os.listdir(f'cogs/{filepath}'):
if filename.endswith('.py'):
filename = filename.replace('.py', '')
allcogs += 1
try:
await bot.load_extension(f'cogs.{filepath}.{filename}')
print_success_message(f'Loaded cogs.{filepath}.{filename}')
loaded += 1
except Exception as error:
print_failure_message(f'Failed to load cogs.{filepath}.{filename}: {error}')
await self.tree.sync()
intents = discord.Intents.default()
intents.members = True
bot = Bot(intents=intents)
bot.remove_command("help")
@bot.event
async def on_ready():
await bot.change_presence(status=discord.Status.idle)
print_success_message(f'Loaded [{loaded}/{allcogs}] cogs')
await bot.change_presence(status=discord.Status.idle, activity=discord.Activity(type=discord.ActivityType.watching, name="anditv.dev"),)
print_success_message(f'has connected as {bot.user} via discord.py {discord.__version__}')
bg_task.start()
@bot.event
async def on_member_join(member):
try:
if greet:
await member.create_dm()
await member.dm_channel.send(f'Welcome **{member.name}** to **{member.guild.name}**!')
except Exception:
pass
@tasks.loop(seconds=5)
async def bg_task():
await bot.wait_until_ready()
counted_members = set()
while not bot.is_closed():
member_count = len(counted_members)
for guild in bot.guilds:
for member in guild.members:
if member.id not in counted_members:
counted_members.add(member.id)
member_count += 1
status_list = [
(discord.Status.dnd, discord.Activity(
type=discord.ActivityType.watching, name="github.com/anditv21")),
(discord.Status.dnd, discord.Activity(
type=discord.ActivityType.watching, name="anditv.dev")),
(discord.Status.dnd, discord.Activity(
type=discord.ActivityType.watching, name=f"{member_count} users")),
(discord.Status.dnd, discord.Activity(
type=discord.ActivityType.watching, name=f"{len(bot.guilds)} servers"))
]
if platform.system() == 'Windows':
status_list[2] = (discord.Status.idle, discord.Activity(
type=discord.ActivityType.playing, name="development mode"))
await bot.change_presence(status=discord.Status.idle, activity=discord.Activity(
type=discord.ActivityType.playing, name="development mode"))
return False
current_index = 0
while current_index < len(status_list):
status, activity = status_list[current_index]
try:
await bot.change_presence(status=status, activity=activity)
await asyncio.sleep(5)
except discord.HTTPException as e:
print(f"Error occurred while changing presence: {e}")
current_index += 1
"""
@bot.event
async def on_message(message):
print(str(message.content))
"""
bot.run(token=token, log_level=40)