forked from itskekoff/discord-server-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
183 lines (144 loc) · 5.42 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- encoding: utf-8 -*-
import logging
import os
import sys
import time
from datetime import datetime
import discord
from discord.ext import commands
from modules.logger import Logger
from modules.configuration import Configuration, check_missing_keys
from modules.updater import Updater
from modules.utilities import get_command_info, format_time
VERSION = "1.4.6"
config_path = "config.json"
data: Configuration = Configuration(config_path)
default_config: dict = {
"token": "Your discord account token",
"prefix": "cp!",
"debug": True,
"clone_settings": {
"name_syntax": "%original%-copy",
"clone_delay": 1.337,
"clear_guild": True,
"icon": True,
"banner": True,
"roles": True,
"channels": True,
"overwrites": True,
"emoji": False,
"stickers": False,
},
"clone_messages": {
"__comment__": "Clone messages in all channels (last messages). Long limit - long time need to copy",
"enabled": True,
"oldest_first": True,
"webhooks_clear": True,
"limit": 8196,
"delay": 0.65,
},
"live_update": {
"__comment__": "Automatically detect new messages and send it via webhook",
"__comment2__": "Also works with clone_messages (starts sending when channel is fully processed)",
"enabled": False,
"process_new_messages": True,
"message_delay": 0.75,
},
}
data.set_default(default=default_config)
logger = Logger()
logger.bind(source="Configuration")
if not data.file_exists(config_path):
data.write_defaults().flush()
logger.error("Configuration doesn't found. Re-created it.")
sys.exit(-1)
new_config, missing_keys = check_missing_keys(config_data=data,
default_data=default_config)
if missing_keys:
logger.error(f"Missing keys {missing_keys} in configuration. Re-created them with default values.")
logger.error("Restart the program to continue.")
sys.exit(-1)
config_values = new_config
token, prefix, debug = (
config_values["token"],
config_values["prefix"],
config_values["debug"],
)
clone_settings_values = config_values["clone_settings"]
clone_messages_values = config_values["clone_messages"]
live_update_values = config_values["live_update"]
name_syntax, clone_delay, clear_guild, clone_icon, clone_banner, clone_roles = (
clone_settings_values["name_syntax"],
clone_settings_values["clone_delay"],
clone_settings_values["clear_guild"],
clone_settings_values["icon"],
clone_settings_values["banner"],
clone_settings_values["roles"],
)
clone_channels, clone_overwrites, clone_emojis, clone_stickers = (
clone_settings_values["channels"],
clone_settings_values["overwrites"],
clone_settings_values["emoji"],
clone_settings_values["stickers"],
)
clone_messages_enabled, clone_oldest_first = (
clone_messages_values["enabled"],
clone_messages_values["oldest_first"],
)
messages_webhook_clear, messages_limit, messages_delay = (
clone_messages_values["webhooks_clear"],
clone_messages_values["limit"],
clone_messages_values["delay"],
)
live_update_enabled, process_new_messages_enabled, live_delay = (
live_update_values["enabled"],
live_update_values["process_new_messages"],
live_update_values["message_delay"]
)
logger = Logger(debug_enabled=debug)
if clone_channels and (not clone_roles and clone_overwrites):
clone_roles = True
logger.warning(
"Clone roles enabled because clone overwrites and channels are enabled."
)
if live_update_enabled and not clone_channels:
logger.error("Live update disabled because clone channels is disabled.")
live_update_enabled = False
if clone_messages_enabled and (messages_limit <= 0):
clone_messages_enabled = False
logger.warning("Messages disabled because its limit is zero.")
bot = commands.Bot(command_prefix=prefix, case_insensitive=True, self_bot=True)
bot.remove_command('help')
logger.reset()
@bot.event
async def on_connect():
logger.success("Logged on as {0.user}".format(bot))
if len(bot.extensions) > 0:
return
for filename in os.listdir('./cogs'):
if filename.endswith('.py') and not filename.startswith('_'):
await bot.load_extension(f'cogs.{filename[:-3]}')
logger.info("Loaded {} extensions, with total of {} commands", len(bot.cogs), len(bot.commands))
@bot.event
async def on_message(message: discord.Message):
await bot.process_commands(message)
@bot.command(name="help")
async def print_help(ctx: commands.Context):
"""
Sends this message
"""
help_message = f"""```\n
* Version: {VERSION}
* Github: github.com/itskekoff/discord-server-copy\n
{get_command_info(bot)}
```
"""
await ctx.message.edit(content=help_message)
if __name__ == "__main__":
updater: Updater = Updater(current_version=VERSION, github_repo="itskekoff/discord-server-copy")
updater.check_for_updates()
file_handler = logging.FileHandler(f'{datetime.now().strftime("%d-%m-%Y")}-discord.log')
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s | %(levelname)s | %(message)s")
logger.info("Logging in discord account...")
bot.run(token, log_handler=file_handler, log_formatter=formatter)