-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathavebot.py
executable file
·272 lines (209 loc) · 9.33 KB
/
avebot.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import discord
from discord.ext import commands
import logging
import logging.handlers
import sys
import traceback
import configparser
import subprocess
import time
import datetime
import socket
import psycopg2
import re
from pathlib import Path
"""AveBot is a bot that does some neat and some dumb stuff."""
log_file_name = "avebot.log"
# Limit of discord (non-nitro) is 8MB (not MiB)
max_file_size = 1000 * 1000 * 8
backup_count = 10000 # random big number
file_handler = logging.handlers.RotatingFileHandler(
filename=log_file_name, maxBytes=max_file_size, backupCount=backup_count)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = logging.Formatter(
'[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s')
file_handler.setFormatter(log_format)
stdout_handler.setFormatter(log_format)
log = logging.getLogger('discord')
log.setLevel(logging.INFO)
log.addHandler(file_handler)
log.addHandler(stdout_handler)
config = configparser.ConfigParser()
config.read("avebot.ini")
postgres_connection = psycopg2.connect(
config['base']['postgres-connection-string'])
gitcomp_regex = r"(?:\s|^)"\
r"(gh|gl|a3|owo|sg|teknik|bb|yt|bc|bcu|sc|aur)"\
r"/([a-zA-Z0-9-_.#/]*)"
gitcomp_long = {"gl": "https://gitlab.com/$link$",
"gh": "https://github.com/$link$",
"a3": "https://git.a3.pm/$link$",
"owo": "https://owo.codes/$link$",
"sg": "https://git.supernets.org/$link$",
"teknik": "https://git.teknik.io/$link$",
"bb": "https://bitbucket.org/$link$",
"yt": "https://youtu.be/$link$",
"bc": "https://$link$.bandcamp.com/",
"bcu": "https://bandcamp.com/$link$",
"sc": "https://soundcloud.com/$link$",
"aur": "https://aur.archlinux.org/packages/$link$"}
def get_prefix(bot, message):
prefixes = [config['base']['prefix']]
return commands.when_mentioned_or(*prefixes)(bot, message)
initial_extensions = ['cogs.common',
'cogs.permissionmanage',
'cogs.emergency',
'cogs.basic',
'cogs.admin',
'cogs.nsfw',
'cogs.technical',
'cogs.finance',
'cogs.imagemanip',
'cogs.fun',
'cogs.emojis',
'cogs.linguistics',
'cogs.stockstream',
'cogs.jose']
bot = commands.Bot(command_prefix=get_prefix,
description=config['base']['description'], pm_help=None)
def get_git_commit_text():
return call_shell("git log -1 --pretty=%B")
def call_shell(command):
return bytes.decode(subprocess.check_output(command,
stderr=subprocess.STDOUT,
shell=True)).strip()
def get_git_revision_short_hash():
return call_shell("git log -1 --pretty=%h")
bot.log = log
bot.config = config
bot.call_shell = call_shell
bot.get_git_revision_short_hash = get_git_revision_short_hash
bot.get_git_commit_text = get_git_commit_text
bot.postgres_connection = postgres_connection
if __name__ == '__main__':
for extension in initial_extensions:
try:
bot.load_extension(extension)
except Exception as e:
log.error(f'Failed to load extension {extension}.', file=sys.stderr)
log.error(traceback.print_exc())
@bot.event
async def on_ready():
log.info(f"\nLogged in as: {bot.user.name} - {bot.user.id}\n"
f"dpy version: {discord.__version__}\n")
bot_activity = discord.Game(name=f"{config['base']['prefix']}help | "
f"{get_git_revision_short_hash()}")
await bot.change_presence(activity=bot_activity)
local_time = str(datetime.datetime.now()).split('.')[0]
total_guild_count = len(bot.guilds)
total_user_count = len(list(bot.get_all_members()))
total_unique_user_count = len(list(set(bot.get_all_members())))
em = discord.Embed(title='AveBot initialized!')
em.add_field(name="Git Hash", value=get_git_revision_short_hash())
em.add_field(name="Last git message", value=get_git_commit_text())
em.add_field(name="Hostname", value=socket.gethostname())
em.add_field(name="Local Time", value=local_time)
em.add_field(name="Guild count", value=total_guild_count)
em.add_field(name="Users", value=total_user_count)
em.add_field(name="Unique users", value=total_unique_user_count)
bot.bot_info = await bot.application_info()
bot.start_time = int(time.time())
bot.main_channel = bot.get_channel(int(config['base']['main-channel']))
bot.support_channel = bot.get_channel(
int(config['base']['support-channel']))
await bot.main_channel.send(embed=em, file=discord.File(log_file_name))
@bot.event
async def on_command(ctx):
log_text = f"{ctx.message.author} ({ctx.message.author.id}):"\
f" \"{ctx.message.content}\" "
if ctx.guild: # was too long for tertiary if
log_text += f"on \"{ctx.channel.name}\" ({ctx.channel.id}) "\
f"at \"{ctx.guild.name}\" ({ctx.guild.id})"
else:
log_text += f"on DMs ({ctx.channel.id})"
log.info(log_text)
@bot.event
async def on_error(event_method, *args, **kwargs):
log.error(f"Error on {event_method}: {sys.exc_info()}")
@bot.event
async def on_command_error(ctx, error):
log.error(f"Error with \"{ctx.message.content}\" from "
f"\"{ctx.message.author}\" ({ctx.message.author.id}): {error}")
@bot.event
async def on_guild_available(guild):
if not bot.is_ready():
return
bot.log.info(f"Guild available: \"{guild.name}\" ({guild.id}).")
em = discord.Embed(title='Guild up', color=0xC0C111)
em.add_field(name="Name", value=guild.name)
em.add_field(name="ID", value=guild.id)
em.set_thumbnail(url=guild.icon_url)
await bot.main_channel.send(embed=em)
@bot.event
async def on_guild_unavailable(guild):
bot.log.info(f"Guild unavailable: \"{guild.name}\" ({guild.id}).")
em = discord.Embed(title='Guild down', color=0xFCD15C)
em.add_field(name="Name", value=guild.name)
em.add_field(name="ID", value=guild.id)
em.set_thumbnail(url=guild.icon_url)
await bot.main_channel.send(embed=em)
@bot.event
async def on_guild_remove(guild):
bot.log.info(f"Guild remove: \"{guild.name}\" ({guild.id}).")
em = discord.Embed(title='Guild got removed / kicked off guild',
color=0xD50000)
em.add_field(name="Name", value=guild.name)
em.add_field(name="ID", value=guild.id)
em.set_thumbnail(url=guild.icon_url)
await bot.main_channel.send(embed=em)
@bot.event
async def on_guild_join(guild):
bot.log.info(f"Joined guild \"{guild.name}\" ({guild.id}).")
em = discord.Embed(title='Joined guild', color=0x1B5E20)
em.add_field(name="Name", value=guild.name)
em.add_field(name="ID", value=guild.id)
em.add_field(name="User Count", value=guild.member_count)
em.add_field(name="Region", value=guild.region)
em.add_field(name="Owner", value=guild.owner)
em.add_field(name="Verification Level", value=guild.verification_level)
em.add_field(name="Created at", value=guild.created_at)
em.set_thumbnail(url=guild.icon_url)
await bot.main_channel.send(embed=em)
await guild.owner.send("Hello and welcome to AveBot!\n"
"If you don't know why you're getting this message, it's because someone "
"added AveBot to your server\nDue to Discord API ToS, I am required to "
"inform you that **I log command usages and errors**.\n**I don't log "
"*anything* else**.\nLogging code can be found at <https://github.com/"
"aveao/AveBot/blob/fea15e7973fb55fc0b2471254182a591370491d2/avebot.py"
"#L99-L119>, please feel free to check it out if you have any concerns."
"\n\nIf you do not agree to be logged, stop using AveBot and remove it "
"from your server as soon as possible.")
@bot.event
async def on_message(message):
if message.author.bot:
return
user_perm = await bot.get_permission(message.author.id)
if user_perm == 0:
return
if str(message.guild.id) in bot.config["shortgit"]\
and bot.config["shortgit"][str(message.guild.id)]:
# Taken from guitar source code, which is based on wbot.
# Both are proprietary testing bots by me.
greg = re.findall(gitcomp_regex, message.content, re.MULTILINE)
greg_completed = []
for gitf in greg:
url = gitcomp_long[gitf[0]].replace("$link$", gitf[1])
greg_completed.append(url)
if greg_completed:
await message.channel.send(' '.join(greg_completed))
return
if message.content.lower().strip() == "now where could my pipe be":
await message.channel.send("garfielf!!")
ctx = await bot.get_context(message)
await bot.invoke(ctx)
if not Path("avebot.ini").is_file():
log.warning(
"No config file (avebot.ini) found, "
"please create one from avebot.ini.example file.")
exit(3)
bot.run(config['base']['token'], bot=True, reconnect=True)