1
|
|
|
import os |
2
|
|
|
import logging |
3
|
|
|
import json |
4
|
|
|
from discord_slash.model import SlashCommandPermissionType |
5
|
|
|
from discord_slash.utils.manage_commands import create_permission |
6
|
|
|
import discord |
7
|
|
|
from discord.ext import commands |
8
|
|
|
from discord_slash import SlashCommand |
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
|
|
|
# Load environment variables |
22
|
|
|
# from dotenv import load_dotenv |
23
|
|
|
# load_dotenv() |
24
|
|
|
# TOKEN = os.getenv("DC_TOKEN") |
25
|
|
|
|
26
|
|
|
# Must be converted to int as python recognizes as str otherwise |
27
|
|
|
# GUILD = int(os.getenv("DC_MAIN_GUILD")) |
28
|
|
|
# CH_MEMBER_COUNT = int(os.getenv("DC_CH_MEMBERS")) |
29
|
|
|
# MODERATION_ROLES_IDS = os.getenv("DC_MODERATION_ROLES") |
30
|
|
|
# CH_LOGS = int(os.getenv("DC_CH_LOGS")) |
31
|
|
|
# added a test command |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
class MyBot(commands.Bot): |
35
|
|
|
|
36
|
|
|
# Init |
37
|
|
|
def __init__(self): |
38
|
|
|
super().__init__(command_prefix="!", intents=intents) |
39
|
|
|
print("[INFO]: Init Bot") |
40
|
|
|
self.GUILD = get_settings("guild") |
41
|
|
|
self.ADMIN = get_settings("ADMIN") |
42
|
|
|
self.MOD_ROLES = get_settings("MOD_ROLES") |
43
|
|
|
self.PERMISSIONS_MODS = { |
44
|
|
|
self.GUILD[0]: [ |
45
|
|
|
create_permission(self.MOD_ROLES[0], SlashCommandPermissionType.ROLE, True), |
46
|
|
|
create_permission(self.MOD_ROLES[1], SlashCommandPermissionType.ROLE, True) |
47
|
|
|
] |
48
|
|
|
} |
49
|
|
|
self.CH_ROLE_REQUEST = get_settings("CH_ROLE_REQUEST") |
50
|
|
|
self.CH_TOTAL_MEMBERS = get_settings("CH_TOTAL_MEMBERS") |
51
|
|
|
self.CH_NIGHTMARE_KILLED = get_settings("CH_NIGHTMARE_KILLED") |
52
|
|
|
self.CH_COMMON = get_settings("CH_COMMON") |
53
|
|
|
self.CH_LOGS = get_settings("CH_LOGS") |
54
|
|
|
self.CH_DISCUSSION_EN = get_settings("CH_DISCUSSION_EN") |
55
|
|
|
self.CAT_SPOTTING = get_settings("CAT_SPOTTING") |
56
|
|
|
|
57
|
|
|
with open('json_files/config.json', 'r', encoding='utf-8-sig') as fp: |
58
|
|
|
# fp.encoding = 'utf-8-sig' |
59
|
|
|
self.config = json.load(fp) |
60
|
|
|
|
61
|
|
|
# On Client Start |
62
|
|
|
async def on_ready(self): |
63
|
|
|
await MyBot.change_presence(self, activity=discord.Activity(type=discord.ActivityType.playing, |
64
|
|
|
name="The Witcher: Monster Slayer")) |
65
|
|
|
print("[INFO]: Bot now online") |
66
|
|
|
|
67
|
|
|
async def update_member_count(self, ctx): |
68
|
|
|
true_member_count = len([m for m in ctx.guild.members if not m.bot]) |
69
|
|
|
new_name = f"Total members: {true_member_count}" |
70
|
|
|
channel = self.get_channel(self.CH_TOTAL_MEMBERS) |
71
|
|
|
await discord.VoiceChannel.edit(channel, name=new_name) |
72
|
|
|
|
73
|
|
|
# On member join |
74
|
|
|
async def on_member_join(self, ctx): |
75
|
|
|
await self.update_member_count(ctx) |
76
|
|
|
print(f"Someone joined") |
77
|
|
|
|
78
|
|
|
# Manage on message actions |
79
|
|
|
async def on_message(self, ctx): |
80
|
|
|
if ctx.author.id == self.user.id: |
81
|
|
|
return |
82
|
|
|
|
83
|
|
|
# If not on role-request channel |
84
|
|
|
if ctx.content.startswith("!") and ctx.channel.id != self.CH_ROLE_REQUEST: |
85
|
|
|
await ctx.channel.send( |
86
|
|
|
fr"{ctx.author.mention} Please use / instead of ! to use commands on the server!", |
87
|
|
|
delete_after=5.0) |
88
|
|
|
await ctx.delete() |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
def main(): |
92
|
|
|
pogmare = MyBot() |
93
|
|
|
print(f"[INFO]: Rate limited: {pogmare.is_ws_ratelimited()}") |
94
|
|
|
|
95
|
|
|
# Allow slash commands |
96
|
|
|
slash = SlashCommand(pogmare, sync_commands=True, sync_on_cog_reload=False) |
97
|
|
|
|
98
|
|
|
# Load cogs |
99
|
|
|
for cog in os.listdir("./cogs"): |
100
|
|
|
if cog.endswith("cog.py"): |
101
|
|
|
try: |
102
|
|
|
cog = f"cogs.{cog.replace('.py', '')}" |
103
|
|
|
pogmare.load_extension(cog) |
104
|
|
|
except Exception as e: |
105
|
|
|
print(f"{cog} Could not be loaded") |
106
|
|
|
raise e |
107
|
|
|
|
108
|
|
|
pogmare.run(get_settings("token")) |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
if __name__ == "__main__": |
112
|
|
|
main() |
113
|
|
|
|