This repository has been archived by the owner on Sep 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
executable file
·163 lines (140 loc) · 5.95 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
# Copyright (c) 2020.
# MIT License
#
# Copyright (c) 2019 YumeNetwork
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import datetime
import json
import logging
import sys
import traceback
import discord
from discord.ext import commands
from modules.utils.error import Errors
with open("./config/config.json", "r") as cjson:
config = json.load(cjson)
with open("./config/token.json", "r") as cjson:
token = json.load(cjson)
modules = config["modules"]
def get_prefix(bot, message):
prefixes = ["--", "y!", "yume", "yum", "yume ", "yum ", "yume!"]
return commands.when_mentioned_or(*prefixes)(bot, message)
description = "Yume Bot ! Peace & Dream <3"
logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)
class YumeBot(commands.Bot):
def __init__(self):
super().__init__(
command_prefix=get_prefix,
description=description,
activity=discord.Game(name="YumeBot..."),
pm_help=None,
help_attrs=dict(hidden=True),
fetch_offline_members=False,
)
self.uptime = datetime.datetime.utcnow()
self.token = token["token"]
self.ready = False
self.config = config
self.owner = config["owner_id"]
self.guild = config["support"]
self.debug = config["debug"]
self.remove_command("help")
async def on_ready(self):
if not self.ready:
self.ready = True
print("Logged in.")
loaded = len(modules)
for module in modules:
try:
self.load_extension("modules." + module)
except Exception as e:
loaded -= 1
print("Failed to load module {} : {}".format(module, e))
traceback.print_exc()
print("{}/{} modules loaded".format(loaded, len(modules)))
async def on_command_error(self, ctx, error):
if isinstance(error, commands.CheckFailure):
em = await Errors.check_error(ctx)
try:
return await ctx.send(embed=em)
except discord.Forbidden:
return
elif isinstance(error, commands.UserInputError):
command = bot.get_command(f"help {ctx.command.name}")
if command:
await ctx.invoke(command)
else:
print(error)
# TODO: Check if there is a group command or something like this
elif isinstance(error, commands.CommandInvokeError):
original = error.original
if not isinstance(original, discord.HTTPException):
print(f"In {ctx.command.qualified_name}:", file=sys.stderr)
traceback.print_tb(original.__traceback__)
print(f"{original.__class__.__name__}: {original}", file=sys.stderr)
elif isinstance(original, discord.Forbidden):
try:
em = await Errors.forbidden_error()
await ctx.send(embed=em)
except discord.Forbidden:
return
elif isinstance(error, commands.CommandOnCooldown):
return await ctx.send("You're on cooldown ! Don't spam this command")
async def close(self):
await super().close()
async def on_guild_join(self, guild):
await self.wait_until_ready()
embed = discord.Embed(colour=discord.Color.green())
embed.title = "New Guild"
embed.set_author(
name="{0} <{0.id}>".format(guild.owner), icon_url=guild.owner.avatar_url
)
embed.add_field(name="Server", value="{0.name} <{0.id}>".format(guild))
embed.add_field(name="Members", value="**{0}**".format(len(guild.members)))
embed.timestamp = datetime.datetime.now()
try:
guild: discord.Guild = self.get_guild(488765635439099914)
except discord.HTTPException:
return
channel: discord.TextChannel = guild.get_channel(int(self.debug))
if isinstance(channel, discord.TextChannel):
await channel.send(embed=embed)
async def on_guild_remove(self, guild):
await self.wait_until_ready()
embed = discord.Embed(colour=discord.Color.red())
embed.title = "Left Guild"
embed.set_author(
name="{0} <{0.id}>".format(guild.owner), icon_url=guild.owner.avatar_url
)
embed.add_field(name="Server", value="{0.name} <{0.id}>".format(guild))
embed.add_field(name="Members", value="**{0}**".format(len(guild.members)))
embed.timestamp = datetime.datetime.now()
try:
guild: discord.Guild = self.get_guild(488765635439099914)
except discord.HTTPException:
return
channel: discord.TextChannel = guild.get_channel(int(self.debug))
if isinstance(channel, discord.TextChannel):
await channel.send(embed=embed)
def run(self):
super().run(self.token, reconnect=True)
bot = YumeBot()
bot.run()