-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
178 lines (152 loc) · 6.39 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
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
import logging
import os
import discord
from discord.ext import commands
from modules import banhammer, filemanager, request, util
logging_level = os.getenv('LOGGING_LEVEL')
level = logging.getLevelName(logging_level)
logging.basicConfig(level=level, filename='log/bot.log')
bot = commands.Bot(command_prefix='.')
bot_token = os.getenv('BOT_TOKEN')
server_ip = os.getenv('SERVER_IP')
mc_version = '1.16.5'
# Discord Channel used for commands:
admin_channel = int(os.getenv('ADMIN_CHANNEL_ID'))
# Discord Channel used for confirming / denying requests:
requests_channel = int(os.getenv('CHANNEL_ID_REQUESTS'))
if os.getenv('RULES_ADDRESS'):
rules_address = os.getenv('RULES_ADDRESS')
requests_messages = []
def take_request(message_id):
for i in requests_messages:
if i.admin_msg_id == message_id:
tmp = i
requests_messages.remove(i)
return tmp
# Bot events:
@bot.event
async def on_reaction_add(reaction, user):
if user.bot:
return
msg = reaction.message
if msg.author != bot.user:
return
admin = bot.get_channel(requests_channel)
current = take_request(msg.id)
await msg.clear_reactions()
dc_user = await bot.fetch_user(current.dc_id)
mc_name = current.mc_name
if reaction.emoji == '✅':
await filemanager.write_whitelist(current)
logging.info('Player whitelisted: {} {} {}'.format(mc_name, current.first_name, current.classs))
embed = discord.Embed(title='Server', color=0x22a7f0)
if rules_address:
embed.add_field(name='Regeln', value=rules_address)
embed.add_field(name='IP', value=server_ip)
embed.add_field(name='Version', value=mc_version)
await dc_user.send(
'Deine Anfrage für den Account `{}` wurde angenommen. Bitte lies dir noch die Regeln durch.'.format(
mc_name),
embed=embed)
await admin.send('The player `{}` was whitelisted.'.format(mc_name))
else:
await admin.send('The request for the player `{}` was denied.'.format(mc_name))
await dc_user.send('Deine Anfrage für den Spieler `{}`wurde abgelehnt. Bei Fragen kontaktiere die '
'Admins im support-Channel auf dem Discord-Server.'.format(mc_name))
@bot.event
async def on_ready():
await bot.change_presence(activity=discord.Game('Minecraft'), status=discord.Status.online)
global requests_messages
requests_messages = filemanager.load_requests()
logging.info('Requests loaded from last sessions: ' + str(requests_messages))
logging.info('Ready, username: {}'.format(bot.user.name))
# Bot commands:
@bot.command()
async def ban(ctx, *args):
if ctx.channel.id != admin_channel:
return
target = args[0]
reason = ' '.join(args[1:])
if len(reason) > 255:
await ctx.send('Reason too long (Max 255 chars)')
return
if '<@!' in target:
# target is expected to be a Discord User
dc_id = target.strip('<@!>')
logging.info('Discord ID to remove from whitelist: ' + dc_id)
banhammer.ban_by_dc_id(dc_id, reason)
await ctx.send('{} has been removed from the whitelist. (Reason: {})'.format(target, reason))
else:
# target is expected to be a Minecraft Username
logging.info('Banning by Minecraft name...')
uuid = util.get_uuid(target)
if not uuid:
await ctx.send('Player `{}` not found.'.format(target))
return
banhammer.ban_by_mc_uuid(uuid, reason)
await ctx.send('MC Acc {} was banned. (Reason: {})'.format(target, reason).format(target))
dc_id = filemanager.get_dc_id(uuid)
dc_user = await bot.fetch_user(dc_id)
ban_infos = filemanager.get_ban_infos(dc_id)
await dc_user.send(
"""Du wurdest vom Minecraft-Server wegen {} gebannt. Fragen --> Support Channel auf dem Discord-Server (unter
Angabe der Bann-ID {})""".format(ban_infos['reason'], ban_infos['ban_id']))
@bot.command()
async def ban_infos(ctx, arg):
infos = filemanager.get_ban_infos_by_id(arg)
if not infos:
await ctx.send('Ban not found.')
return
embed = discord.Embed(title='Ban Infos', color=0x22a7f0)
embed.add_field(name='Reason', value=infos['reason'])
embed.add_field(name='Date of ban', value=infos['date_banned'])
embed.add_field(name='User', value='<@!{}>'.format(infos['dc_id']))
await ctx.send(embed=embed)
@bot.command()
async def shutdown(ctx):
if ctx.channel.id != admin_channel:
return
await bot.change_presence(status=discord.Status.offline)
filemanager.save_requests(requests_messages)
await bot.logout()
@bot.command()
async def test(ctx, args):
print(str(args))
@bot.command()
async def whitelist(ctx, arg1, arg2, arg3):
logging.info('Request incoming...')
mc_name = arg1
first_name = arg2
classs = arg3
member = ctx.author
await ctx.message.delete()
logging.debug('Fetching UUID...')
uuid = util.get_uuid(mc_name)
if not uuid:
await ctx.send('Der Spieler `{}` wurde nicht gefunden {}.'.format(mc_name, member.mention))
return
ids_in_db_amount = filemanager.ids_in_db(uuid, member.id)
if ids_in_db_amount['amount_mc'] > 0:
await ctx.send('Der Spieler `{}` ist bereits registriert {}.'.format(mc_name, member.mention))
return
admins = bot.get_channel(requests_channel)
embed = discord.Embed(title='Whitelist-Anfrage', color=0x22a7f0)
embed.add_field(name='von', value=member.mention)
embed.add_field(name='MC-Username', value=mc_name)
embed.add_field(name='Vorname', value=first_name)
embed.add_field(name='Klasse', value=classs)
embed.add_field(name='Von diesem User bereits gewhitelistet', value=ids_in_db_amount['amount_dc'])
admin_msg = await admins.send(embed=embed)
await admin_msg.add_reaction('✅')
await admin_msg.add_reaction('❌')
requests_messages.append(request.WhitelistRequest(member.id, admin_msg.id, mc_name, uuid, first_name, classs))
await ctx.send('Deine Anfrage für `{}` wurde versandt {}.'.format(mc_name, member.mention))
logging.info('Request successful')
# Errors:
@whitelist.error
async def whitelist_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
logging.error(error)
await ctx.send(
'Bitte benutze `.whitelist Minecraft_username Vorname Klasse.` (keine Leerzeichen innerhalb der Argumente)')
bot.run(bot_token)