1
|
|
|
import inspect |
2
|
|
|
import os |
3
|
|
|
import logging |
4
|
|
|
import json |
5
|
|
|
import discord |
6
|
|
|
from discord.ext import commands, tasks |
7
|
|
|
from discord_slash import SlashCommand |
8
|
|
|
from modules.get_settings import get_settings |
9
|
|
|
from datetime import datetime |
10
|
|
|
|
11
|
|
|
# Create logger |
12
|
|
|
logger = logging.getLogger('discord') |
13
|
|
|
logger.setLevel(logging.DEBUG) |
14
|
|
|
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w') |
15
|
|
|
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) |
16
|
|
|
logger.addHandler(handler) |
17
|
|
|
|
18
|
|
|
# Set bot privileges |
19
|
|
|
intents = discord.Intents.all() |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class MyBot(commands.Bot): |
23
|
|
|
|
24
|
|
|
# Init |
25
|
|
|
def __init__(self): |
26
|
|
|
super().__init__(command_prefix="!", intents=intents) |
27
|
|
|
print(f"[{self.__class__.__name__}]: Init") |
28
|
|
|
print(f"[{self.__class__.__name__}]: Rate limited: {self.is_ws_ratelimited()}") |
29
|
|
|
self.guild = get_settings("guild") |
30
|
|
|
self.ch_role_request = get_settings("CH_ROLE_REQUEST") |
31
|
|
|
self.ch_total_members = get_settings("CH_TOTAL_MEMBERS") |
32
|
|
|
self.ch_nightmare_killed = get_settings("CH_NIGHTMARE_KILLED") |
33
|
|
|
self.ch_leaderboards = get_settings("CH_LEADERBOARDS") |
34
|
|
|
self.ch_leaderboards_common = get_settings("CH_LEADERBOARDS_COMMON") |
35
|
|
|
self.ch_common = get_settings("CH_COMMON") |
36
|
|
|
self.ch_logs = get_settings("CH_LOGS") |
37
|
|
|
self.ch_discussion_en = get_settings("CH_DISCUSSION_EN") |
38
|
|
|
self.cat_spotting = get_settings("CAT_SPOTTING") |
39
|
|
|
self.update_ch_commons_loop.start() |
40
|
|
|
|
41
|
|
|
with open('server_files/config.json', 'r', encoding='utf-8-sig') as fp: |
42
|
|
|
# fp.encoding = 'utf-8-sig' |
43
|
|
|
self.config = json.load(fp) |
44
|
|
|
|
45
|
|
|
# On Client Start |
46
|
|
|
async def on_ready(self): |
47
|
|
|
await MyBot.change_presence(self, activity=discord.Activity(type=discord.ActivityType.playing, |
48
|
|
|
name="The Witcher: Monster Slayer")) |
49
|
|
|
|
50
|
|
|
async def update_member_count(self, ctx): |
51
|
|
|
true_member_count = len([m for m in ctx.guild.members if not m.bot]) |
52
|
|
|
new_name = f"Total members: {true_member_count}" |
53
|
|
|
channel = self.get_channel(self.ch_total_members) |
54
|
|
|
await discord.VoiceChannel.edit(channel, name=new_name) |
55
|
|
|
|
56
|
|
|
# On member join |
57
|
|
|
async def on_member_join(self, ctx): |
58
|
|
|
await self.update_member_count(ctx) |
59
|
|
|
print(f"Someone joined") |
60
|
|
|
|
61
|
|
|
# Manage on message actions |
62
|
|
|
async def on_message(self, ctx): |
63
|
|
|
if ctx.author.id == self.user.id: |
64
|
|
|
return |
65
|
|
|
|
66
|
|
|
# If not on role-request channel |
67
|
|
|
if ctx.content.startswith("!") and ctx.channel.id != self.ch_role_request: |
68
|
|
|
await ctx.channel.send( |
69
|
|
|
fr"{ctx.author.mention} Please use / instead of ! to use commands on the server!", |
70
|
|
|
delete_after=5.0) |
71
|
|
|
await ctx.delete() |
72
|
|
|
|
73
|
|
|
# Loop tasks |
74
|
|
|
# Update common spotting channel name |
75
|
|
|
async def update_ch_commons(self): |
76
|
|
|
with open('./server_files/commons.txt') as f: |
77
|
|
|
try: |
78
|
|
|
commons = f.read().splitlines() |
79
|
|
|
except ValueError: |
80
|
|
|
print(ValueError) |
81
|
|
|
|
82
|
|
|
new_name = f"common-{commons[0]}" |
83
|
|
|
channel = self.get_channel(self.ch_common) |
84
|
|
|
await discord.TextChannel.edit(channel, name=new_name) |
85
|
|
|
print(f"[{self.__class__.__name__}]: Common channel name updated: {new_name}") |
86
|
|
|
|
87
|
|
|
commons.append(commons.pop(commons.index(commons[0]))) |
88
|
|
|
with open('./server_files/commons.txt', 'w') as f: |
89
|
|
|
for item in commons: |
90
|
|
|
f.write("%s\n" % item) |
91
|
|
|
|
92
|
|
|
# Update commons channel name every day at 12:00(?) |
93
|
|
|
@tasks.loop(minutes=60.0) |
94
|
|
|
async def update_ch_commons_loop(self): |
95
|
|
|
if datetime.now().hour == 12: |
96
|
|
|
await self.update_ch_commons() |
97
|
|
|
|
98
|
|
|
@update_ch_commons_loop.before_loop |
99
|
|
|
async def before_update_ch_commons(self): |
100
|
|
|
await self.wait_until_ready() |
101
|
|
|
|
102
|
|
|
# Disconnect Bot using "!" prefix (For safety reasons in case Slash commands are not working |
103
|
|
|
@commands.command(name="ex", pass_context=True, aliases=["e", "exit"]) |
104
|
|
|
async def exit_bot(self, ctx): |
105
|
|
|
await ctx.send(f"Closing Bot", delete_after=1.0) |
106
|
|
|
print(f"[{self.__class__.__name__}]: Exiting Bot") |
107
|
|
|
await self.close() |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def main(): |
111
|
|
|
pogmare = MyBot() |
112
|
|
|
|
113
|
|
|
# Allow slash commands |
114
|
|
|
slash = SlashCommand(pogmare, sync_commands=True, sync_on_cog_reload=False) |
115
|
|
|
|
116
|
|
|
# Load cogs |
117
|
|
|
for cog in os.listdir("./cogs"): |
118
|
|
|
if cog.endswith("cog.py"): |
119
|
|
|
try: |
120
|
|
|
cog = f"cogs.{cog.replace('.py', '')}" |
121
|
|
|
pogmare.load_extension(cog) |
122
|
|
|
except Exception as e: |
123
|
|
|
print(f"{cog} Could not be loaded") |
124
|
|
|
raise e |
125
|
|
|
|
126
|
|
|
pogmare.run(get_settings("token")) |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
if __name__ == "__main__": |
130
|
|
|
main() |
131
|
|
|
|