1
|
|
|
import json |
2
|
|
|
import logging |
3
|
|
|
import os |
4
|
|
|
from datetime import datetime |
5
|
|
|
import discord |
6
|
|
|
from discord.ext import commands, tasks |
7
|
|
|
from discord_slash import SlashCommand |
8
|
|
|
|
9
|
|
|
from modules.get_settings import get_settings |
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
|
|
|
def __init__(self): |
25
|
|
|
super().__init__(command_prefix="!", intents=intents) |
26
|
|
|
|
27
|
|
|
dt_string = self.get_current_time() |
28
|
|
|
print(f"({dt_string})\t[{self.__class__.__name__}]: Init") |
29
|
|
|
print(f"({dt_string})\t[{self.__class__.__name__}]: Rate limited: {self.is_ws_ratelimited()}") |
30
|
|
|
|
31
|
|
|
self.guild = get_settings("guild") |
32
|
|
|
self.ch_admin_posting = get_settings("CH_ADMIN_POSTING") |
33
|
|
|
self.ch_role_request = get_settings("CH_ROLE_REQUEST") |
34
|
|
|
self.ch_total_members = get_settings("CH_TOTAL_MEMBERS") |
35
|
|
|
self.ch_nightmare_killed = get_settings("CH_NIGHTMARE_KILLED") |
36
|
|
|
self.ch_leaderboards = get_settings("CH_LEADERBOARDS") |
37
|
|
|
self.ch_leaderboards_common = get_settings("CH_LEADERBOARDS_COMMON") |
38
|
|
|
self.ch_leaderboards_event = get_settings("CH_LEADERBOARDS_EVENT") |
39
|
|
|
self.ch_common = get_settings("CH_COMMON") |
40
|
|
|
self.ch_logs = get_settings("CH_LOGS") |
41
|
|
|
self.ch_discussion_en = get_settings("CH_DISCUSSION_EN") |
42
|
|
|
self.cat_spotting = get_settings("CAT_SPOTTING") |
43
|
|
|
self.update_ch_commons_loop.start() |
44
|
|
|
|
45
|
|
|
with open('server_files/config.json', 'r', encoding='utf-8-sig') as fp: |
46
|
|
|
self.config = json.load(fp) |
47
|
|
|
|
48
|
|
|
# On bot ready |
49
|
|
|
async def on_ready(self): |
50
|
|
|
await MyBot.change_presence(self, activity=discord.Activity(type=discord.ActivityType.playing, |
51
|
|
|
name="The Witcher: Monster Slayer")) |
52
|
|
|
dt_string = self.get_current_time() |
53
|
|
|
print(f"({dt_string})\t[{self.__class__.__name__}]: Bot ready") |
54
|
|
|
|
55
|
|
|
async def update_member_count(self, ctx): |
56
|
|
|
true_member_count = len([m for m in ctx.guild.members if not m.bot]) |
57
|
|
|
new_name = f"Total members: {true_member_count}" |
58
|
|
|
channel = self.get_channel(self.ch_total_members) |
59
|
|
|
await discord.VoiceChannel.edit(channel, name=new_name) |
60
|
|
|
|
61
|
|
|
# On member join |
62
|
|
|
async def on_member_join(self, ctx, member: discord.Member = None): |
63
|
|
|
await self.update_member_count(ctx) |
64
|
|
|
dt_string = self.get_current_time() |
65
|
|
|
print(f"{dt_string})\t[{self.__class__.__name__}]: {ctx.display_name} joined") |
66
|
|
|
|
67
|
|
|
# Manage on message actions |
68
|
|
|
async def on_message(self, ctx): |
69
|
|
|
# If bot is the message author |
70
|
|
|
if ctx.author.id == self.user.id: |
71
|
|
|
return |
72
|
|
|
|
73
|
|
|
# If there is a message with "!" prefix |
74
|
|
|
if ctx.content.startswith("!") and ctx.channel.id != self.ch_role_request: |
75
|
|
|
await ctx.channel.send( |
76
|
|
|
fr"{ctx.author.mention} Please use / instead of ! to use commands on the server!", |
77
|
|
|
delete_after=5.0) |
78
|
|
|
await ctx.delete() |
79
|
|
|
|
80
|
|
|
# Loop tasks |
81
|
|
|
# Update common spotting channel name |
82
|
|
|
async def update_ch_commons(self): |
83
|
|
|
with open('./server_files/commons.txt') as f: |
84
|
|
|
try: |
85
|
|
|
commons = f.read().splitlines() |
86
|
|
|
except ValueError: |
87
|
|
|
print(ValueError) |
88
|
|
|
|
89
|
|
|
new_name = f"common {commons[0]}" |
90
|
|
|
common_ch = self.get_channel(self.ch_common) |
91
|
|
|
await discord.TextChannel.edit(common_ch, name=new_name) |
92
|
|
|
dt_string = self.get_current_time() |
93
|
|
|
print(f"({dt_string})\t[{self.__class__.__name__}]: Common channel name updated: {commons[0]}") |
94
|
|
|
|
95
|
|
|
admin_posting = self.get_channel(self.ch_admin_posting) |
96
|
|
|
await admin_posting.send(f"Common changed: {commons[0]}") |
97
|
|
|
|
98
|
|
|
commons.append(commons.pop(commons.index(commons[0]))) |
99
|
|
|
with open('./server_files/commons.txt', 'w') as f: |
100
|
|
|
for item in commons: |
101
|
|
|
f.write("%s\n" % item) |
102
|
|
|
|
103
|
|
|
# Update commons channel name every day at 12:00 |
104
|
|
|
@tasks.loop(minutes=60.0) |
105
|
|
|
async def update_ch_commons_loop(self): |
106
|
|
|
if datetime.now().hour == 12: |
107
|
|
|
await self.update_ch_commons() |
108
|
|
|
|
109
|
|
|
@update_ch_commons_loop.before_loop |
110
|
|
|
async def before_update_ch_commons(self): |
111
|
|
|
await self.wait_until_ready() |
112
|
|
|
|
113
|
|
|
# Disconnect Bot using "!" prefix (For safety reasons in case Slash commands are not working |
114
|
|
|
@commands.command(name="ex", pass_context=True, aliases=["e", "exit"]) |
115
|
|
|
async def exit_bot(self, ctx): |
116
|
|
|
await ctx.send(f"Closing Bot", delete_after=1.0) |
117
|
|
|
dt_string = self.get_current_time() |
118
|
|
|
print(f"({dt_string})\t[{self.__class__.__name__}]: Exiting Bot") |
119
|
|
|
await self.close() |
120
|
|
|
|
121
|
|
|
@staticmethod |
122
|
|
|
def get_current_time(): |
123
|
|
|
now = datetime.now() |
124
|
|
|
dt_string = now.strftime("%d/%m/%Y %H:%M:%S") |
125
|
|
|
return dt_string |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
def main(): |
129
|
|
|
pogmare = MyBot() |
130
|
|
|
|
131
|
|
|
# Allow slash commands |
132
|
|
|
slash = SlashCommand(pogmare, sync_commands=True, sync_on_cog_reload=False) |
133
|
|
|
|
134
|
|
|
# Load cogs |
135
|
|
|
for cog in os.listdir("./cogs"): |
136
|
|
|
if cog.endswith("cog.py"): |
137
|
|
|
try: |
138
|
|
|
cog = f"cogs.{cog.replace('.py', '')}" |
139
|
|
|
pogmare.load_extension(cog) |
140
|
|
|
except Exception as e: |
141
|
|
|
print(f"{cog} Could not be loaded") |
142
|
|
|
raise e |
143
|
|
|
|
144
|
|
|
pogmare.run(get_settings("token")) |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
if __name__ == "__main__": |
148
|
|
|
main() |
149
|
|
|
|