This repository has been archived by the owner on Jan 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
152 lines (119 loc) · 5.25 KB
/
bot.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
from discord.ext import commands
from discord import Embed, Activity, ActivityType, Status, Streaming, Game
from botmodules import serverfiles, apis
#
extensionfolder = "botcmds"
extensions = ['basic','support','moderation','games','help','channels','music','owneronly','converters','embedgenerator']
sudo_ids = [285832847409807360]
sudo_seperator = "--sudo"
all_prefixes = ["/","!","$",".","-",">","?"]
# Own functions
def get_prefix(client, message):
if message.guild:
prefixes = ['/']
else:
prefixes = all_prefixes
return commands.when_mentioned_or(*prefixes)(client, message)
# Own classes
class MyContext(commands.Context):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.apis = apis
if self.guild is not None:
self.data = serverfiles.Server.getServer(self.guild.id)
## manupulate ctx for --sudo arg
if int(self.author.id) in sudo_ids:
if sudo_seperator in self.message.content:
try:
msg = self.message.content
newmsg = msg.split(sudo_seperator)[0]
newmember = msg.split(sudo_seperator)[1]
self.message.content = newmsg
userid = int(newmember.strip().lstrip("<@").lstrip("!").lstrip("&").rstrip(">") if "<@" in newmember and ">" in newmember else newmember)
member = self.guild.get_member(userid)
self.author = member
self.message.author = member
except (ValueError, ) as e:
print("[SUDO] - Kein gültiges Mitglied: "+newmember+" - Fehler: "+e)
def getargs(self, raiserrorwhenmissing=False):
msg = self.message.content.split(" ")
calledbymention = bool(self.prefix in all_prefixes)
length = len(self.args)+len(self.kwargs)-int(calledbymention)
txt = (" ".join(msg[length::])) if len(msg) > length else ""
newmessage = txt.split(sudo_seperator)[0].strip()
if not newmessage and raiserrorwhenmissing:
raise commands.BadArgument(message="Du hast ein wichtiges Argument vergessen!")
return newmessage
async def sendEmbed(self, *args, message:str="", **kwargs):
return await self.send(message, embed=self.getEmbed(*args, **kwargs))
def getEmbed(self, title:str, description:str="", color:int=0x000000, fields:list=[], inline=True, thumbnailurl:str=None, authorurl:str="", authorname:str=None, footertext:str="Angefordert von USER", footerurl:str="AVATARURL", timestamp=False):
EMBED = Embed(title=title, description=description, color=color)
EMBED.set_footer(text=footertext.replace("USER", str(self.author.name+"#"+self.author.discriminator)), icon_url=footerurl.replace("AVATARURL", str(self.author.avatar_url)))
if timestamp:
EMBED.timestamp = datetime.utcnow() if timestamp is True else timestamp
for field in fields:
EMBED.add_field(name=field[0], value=field[1], inline=bool(field[2] if len(field) > 2 else inline))
if thumbnailurl:
EMBED.set_thumbnail(url=thumbnailurl.strip())
if authorname:
if authorurl and ("https://" in authorurl or "http://" in authorurl):
EMBED.set_author(name=authorname, url=authorurl.strip())
else:
EMBED.set_author(name=authorname)
return EMBED
async def tick(self, value):
emoji = '\N{WHITE HEAVY CHECK MARK}' if value else '\N{CROSS MARK}'
try:
await self.message.add_reaction(emoji)
except discord.HTTPException:
pass
class MyBot(commands.Bot):
async def get_context(self, message, *, cls=MyContext):
return await super().get_context(message, cls=cls)
# create Bot
bot = MyBot(
command_prefix=get_prefix,
description='Das ist eine Beschreibung!',
case_insensitive=True,
activity=Activity(type=ActivityType.listening, name="/help"),
status=Status.idle
)
# Events
from botevents.on_voice_state_update import setup as setup_on_voice_state_update
from botevents.on_command_error import setup as setup_on_command_error
setup_on_voice_state_update(bot)
setup_on_command_error(bot)
@bot.event
async def on_ready():
print(f"[Bot] - Logged in as '{bot.user.name}' - '{bot.user.id}'")
bot.remove_command('help')
for extension in extensions:
try:
bot.load_extension(extensionfolder+"."+extension)
except commands.errors.ExtensionAlreadyLoaded:
pass
return
@bot.event
async def on_command(ctx):
#print(f"[Command] - '{ctx.message.content}' von '{ctx.author.name}#{str(ctx.author.discriminator)}'")
if ctx.guild is not None:
try:
await ctx.message.delete()
except:
pass
# Hidden commands
@bot.command(aliases=["."])
async def destroy(ctx):
pass
# Start
def run(TOKEN):
bot.run(TOKEN,bot=True,reconnect=True)
import sys, os
if __name__ == "__main__":
if 'DISCORD_RAFAELSBOT' in os.environ:
run(os.environ.get('DISCORD_RAFAELSBOT'))
elif len(sys.argv) > 1:
run(sys.argv[1])
else:
print("[Bot] - No TOKEN found! Enter it manually...")
run(input("TOKEN: "))