1
|
|
|
import json |
2
|
|
|
import logging |
3
|
|
|
import os |
4
|
|
|
from datetime import datetime |
5
|
|
|
from time import time |
6
|
|
|
|
7
|
|
|
import discord |
8
|
|
|
from discord.ext import commands, tasks |
9
|
|
|
from discord_slash import SlashCommand, utils |
10
|
|
|
|
11
|
|
|
from modules.get_settings import get_settings |
12
|
|
|
|
13
|
|
|
# Create logger |
14
|
|
|
logger = logging.getLogger('discord') |
15
|
|
|
logger.setLevel(logging.DEBUG) |
16
|
|
|
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w') |
17
|
|
|
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) |
18
|
|
|
logger.addHandler(handler) |
19
|
|
|
|
20
|
|
|
# Set bot privileges |
21
|
|
|
intents = discord.Intents.all() |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class MyBot(commands.Bot): |
25
|
|
|
|
26
|
|
|
def __init__(self): |
27
|
|
|
super().__init__(command_prefix="!", intents=intents) |
28
|
|
|
|
29
|
|
|
self.create_main_log_msg('\n --------------- STARTING BOT ---------------') |
30
|
|
|
self.create_main_log_msg('Init') |
31
|
|
|
self.create_main_log_msg(f"Rate limited: {self.is_ws_ratelimited()}") |
32
|
|
|
self.start_time = time() |
33
|
|
|
self.version = "1.2" |
34
|
|
|
|
35
|
|
|
self.guild = get_settings("guild") |
36
|
|
|
self.ch_admin_posting = get_settings("CH_ADMIN_POSTING") |
37
|
|
|
self.ch_role_request = get_settings("CH_ROLE_REQUEST") |
38
|
|
|
self.ch_total_members = get_settings("CH_TOTAL_MEMBERS") |
39
|
|
|
self.ch_nightmare_killed = get_settings("CH_NIGHTMARE_KILLED") |
40
|
|
|
self.ch_guides = get_settings("CH_GUIDES") |
41
|
|
|
|
42
|
|
|
self.ch_leaderboards = get_settings("CH_LEADERBOARDS") |
43
|
|
|
self.ch_leaderboards_common = get_settings("CH_LEADERBOARDS_COMMON") |
44
|
|
|
self.ch_leaderboards_event = get_settings("CH_LEADERBOARDS_EVENT") |
45
|
|
|
|
46
|
|
|
self.cat_spotting = get_settings("CAT_SPOTTING") |
47
|
|
|
self.ch_legendary_spot = get_settings("CH_LEGENDARY_SPOT") |
48
|
|
|
self.ch_legendary_nemeton = get_settings("CH_LEGENDARY_NEMETON") |
49
|
|
|
self.ch_rare_spot = get_settings("CH_RARE_SPOT") |
50
|
|
|
self.ch_rare_nemeton = get_settings("CH_RARE_NEMETON") |
51
|
|
|
self.ch_common = get_settings("CH_COMMON") |
52
|
|
|
self.ch_werewolf = get_settings("CH_WEREWOLF") |
53
|
|
|
self.ch_wraiths = get_settings("CH_WRAITHS") |
54
|
|
|
self.ch_nemeton = get_settings("CH_NEMETON") |
55
|
|
|
|
56
|
|
|
self.ch_logs = get_settings("CH_LOGS") |
57
|
|
|
self.ch_discussion_en = get_settings("CH_DISCUSSION_EN") |
58
|
|
|
self.ch_spotting_stats = get_settings("CH_SPOTTING_STATS") |
59
|
|
|
|
60
|
|
|
self.update_ch_commons_loop.start() |
61
|
|
|
|
62
|
|
|
with open('server_files/config.json', 'r', encoding='utf-8-sig') as fp: |
63
|
|
|
self.config = json.load(fp) |
64
|
|
|
|
65
|
|
|
def create_main_log_msg(self, message: str) -> None: |
66
|
|
|
dt_string = self.get_current_time() |
67
|
|
|
log: str = f"({dt_string}) [{self.__class__.__name__}]:\t\t{message}" |
68
|
|
|
print(log) |
69
|
|
|
logs_txt_dir: str = "logs/logs.txt" |
70
|
|
|
with open(logs_txt_dir, "a+") as file_object: |
71
|
|
|
file_object.write(f"{log}\n") |
72
|
|
|
|
73
|
|
|
# On bot ready |
74
|
|
|
async def on_ready(self) -> None: |
75
|
|
|
await MyBot.change_presence(self, activity=discord.Activity(type=discord.ActivityType.playing, |
76
|
|
|
name="The Witcher: Monster Slayer")) |
77
|
|
|
self.create_main_log_msg("Bot is ready") |
78
|
|
|
|
79
|
|
|
async def update_member_count(self, ctx) -> int: |
80
|
|
|
true_member_count = len([m for m in ctx.guild.members if not m.bot]) |
81
|
|
|
new_name = f"Total members: {true_member_count}" |
82
|
|
|
channel_tm = self.get_channel(self.ch_total_members) |
83
|
|
|
await discord.VoiceChannel.edit(channel_tm, name=new_name) |
84
|
|
|
return true_member_count |
85
|
|
|
|
86
|
|
|
# On member join |
87
|
|
|
async def on_member_join(self, ctx) -> None: |
88
|
|
|
member_count = await self.update_member_count(ctx) |
89
|
|
|
self.create_main_log_msg(f"{ctx} joined") |
90
|
|
|
|
91
|
|
|
if (member_count % 100) == 0: |
92
|
|
|
channel = self.get_channel(self.ch_admin_posting) |
93
|
|
|
await channel.send(f"{member_count} members <:POGMARE:872496675434942484>") |
94
|
|
|
|
95
|
|
|
async def on_member_remove(self, ctx) -> None: |
96
|
|
|
await self.update_member_count(ctx) |
97
|
|
|
self.create_main_log_msg(f"{ctx} left") |
98
|
|
|
|
99
|
|
|
# Manage on message actions |
100
|
|
|
async def on_message(self, ctx) -> None: |
101
|
|
|
# If bot is the message author |
102
|
|
|
if ctx.author.id == self.user.id: |
103
|
|
|
return |
104
|
|
|
if isinstance(ctx.channel, discord.channel.DMChannel): |
105
|
|
|
await ctx.channel.send("If you have any questions please ask my creator - BonJowi#0119") |
106
|
|
|
return |
107
|
|
|
|
108
|
|
|
# If there is a message with "!" prefix |
109
|
|
|
if ctx.content.startswith("!") and "help" not in ctx.content: |
110
|
|
|
await ctx.channel.send( |
111
|
|
|
fr"{ctx.author.mention} Please use / instead of ! to use commands on this server!", |
112
|
|
|
delete_after=5.0) |
113
|
|
|
await ctx.delete() |
114
|
|
|
|
115
|
|
|
# Loop tasks |
116
|
|
|
# Update common spotting channel name |
117
|
|
|
async def update_ch_commons(self) -> None: |
118
|
|
|
with open('./server_files/commons.txt') as f: |
119
|
|
|
try: |
120
|
|
|
commons = f.read().splitlines() |
121
|
|
|
except ValueError: |
122
|
|
|
print(ValueError) |
123
|
|
|
|
124
|
|
|
new_name = f"common {commons[0]}" |
125
|
|
|
common_ch = self.get_channel(self.ch_common) |
126
|
|
|
await discord.TextChannel.edit(common_ch, name=new_name) |
127
|
|
|
self.create_main_log_msg(f"Common channel name updated: {commons[0]}") |
128
|
|
|
await common_ch.send(f"Common changed: **{commons[0]}**") |
129
|
|
|
|
130
|
|
|
commons.append(commons.pop(commons.index(commons[0]))) |
131
|
|
|
with open('./server_files/commons.txt', 'w') as f: |
132
|
|
|
for item in commons: |
133
|
|
|
f.write("%s\n" % item) |
134
|
|
|
|
135
|
|
|
# Update commons channel name every day at 12:00 |
136
|
|
|
@tasks.loop(minutes=60.0) |
137
|
|
|
async def update_ch_commons_loop(self) -> None: |
138
|
|
|
if datetime.utcnow().hour == 14: |
139
|
|
|
await self.update_ch_commons() |
140
|
|
|
|
141
|
|
|
@update_ch_commons_loop.before_loop |
142
|
|
|
async def before_update_ch_commons(self) -> None: |
143
|
|
|
await self.wait_until_ready() |
144
|
|
|
|
145
|
|
|
@staticmethod |
146
|
|
|
def get_current_time() -> str: |
147
|
|
|
now = datetime.utcnow() |
148
|
|
|
return now.strftime("%d/%m/%Y %H:%M:%S") + " UTC" |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
def main() -> None: |
152
|
|
|
pogmare = MyBot() |
153
|
|
|
|
154
|
|
|
# Allow slash commands |
155
|
|
|
slash = SlashCommand(pogmare, sync_commands=False, sync_on_cog_reload=False) |
156
|
|
|
|
157
|
|
|
# Load cogs |
158
|
|
|
for cog in os.listdir("./cogs"): |
159
|
|
|
if cog.endswith("cog.py"): |
160
|
|
|
try: |
161
|
|
|
cog = f"cogs.{cog.replace('.py', '')}" |
162
|
|
|
pogmare.load_extension(cog) |
163
|
|
|
except Exception as e: |
164
|
|
|
print(f"{cog} Could not be loaded") |
165
|
|
|
raise e |
166
|
|
|
|
167
|
|
|
pogmare.run(get_settings("token")) |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
if __name__ == "__main__": |
171
|
|
|
main() |
172
|
|
|
|