forked from bbedward/Beatrice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
628 lines (562 loc) · 22.2 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
import datetime
import re
import api
import settings
import db
import util
import paginator
logger = util.get_logger("main")
BOT_VERSION = "0.2"
# Spam Threshold (Seconds) - how long to output certain commands (e.g. price)
SPAM_THRESHOLD=300
# Change command prefix to whatever you want to begin commands with
COMMAND_PREFIX=settings.command_prefix
# HELP menu header
AUTHOR_HEADER="Beatrice v{0} (BA/NANO Utility Bot)".format(BOT_VERSION)
# Command DOC (TRIGGER, CMD, Overview, Info)
'''
CMD: Command trigger
INFO: Command usage
'''
### Commands for everyone
PRICE = {
"CMD" : "{0}price".format(COMMAND_PREFIX),
"INFO" : "Display NANO price information from a few of the top exchanges"
}
MEME = {
"CMD" : "{0}meme".format(COMMAND_PREFIX),
"INFO" : "Display next meme in sequence"
}
MEMELIST = {
"CMD" : "{0}memelist".format(COMMAND_PREFIX),
"INFO" : "Receive private message with a list of all memes stored with the bot"
}
PUP = {
"CMD" : "{0}pup".format(COMMAND_PREFIX),
"INFO" : "Display next pup in sequence"
}
PUPLIST = {
"CMD" : "{0}puplist".format(COMMAND_PREFIX),
"INFO" : "Receive private message with a list of all pups stored with the bot"
}
MEOW = {
"CMD" : "{0}meow".format(COMMAND_PREFIX),
"INFO" : "Display next meow in sequence"
}
MEOWLIST = {
"CMD" : "{0}meowlist".format(COMMAND_PREFIX),
"INFO" : "Receive private message with a list of all meows stored with the bot"
}
### Admin commands
ADDPUP = {
"CMD" : "{0}addpup, takes: url, author, title".format(COMMAND_PREFIX),
"INFO" : "Add URL to the bot's pup list",
"USAGE" : "{0}addpup <url> <author> <title>".format(COMMAND_PREFIX)
}
ADDMEME = {
"CMD" : "{0}addmeme, takes: url, author, title".format(COMMAND_PREFIX),
"INFO" : "Add URL to the bot's meme list",
"USAGE" : "{0}addmeme <url> <author> <title>".format(COMMAND_PREFIX)
}
ADDMEOW = {
"CMD" : "{0}addmeow, takes: url, author, title".format(COMMAND_PREFIX),
"INFO" : "Add URL to the bot's meow list",
"USAGE" : "{0}addmeow <url> <author> <title>".format(COMMAND_PREFIX)
}
REMOVEPUP = {
"CMD" : "{0}removepup, takes: url or id".format(COMMAND_PREFIX),
"INFO" : "Remove pup matching URL or ID from the bot's pup list"
}
REMOVEMEME = {
"CMD" : "{0}removememe, takes: url or id".format(COMMAND_PREFIX),
"INFO" : "Remove meme matching URL or ID from the bot's meme list"
}
REMOVEMEOW = {
"CMD" : "{0}removemeow, takes: url or id".format(COMMAND_PREFIX),
"INFO" : "Remove meow matching URL or ID from the bot's meow list"
}
MUTE = {
"CMD" : "{0}mute or {0}muzzle, takes: user, duration (optional)".format(COMMAND_PREFIX),
"INFO" : "mute @bbedward 60 = mute bbedward for 60 minutes"
}
UNMUTE = {
"CMD" : "{0}unmute or {0}unmuzzle, user mention".format(COMMAND_PREFIX),
"INFO" : "Unmute mentioned user(s)"
}
### Dictionary of different command categories
COMMANDS = {
"USER_COMMANDS" : [PRICE, MEME, MEMELIST, PUP, PUPLIST, MEOW, MEOWLIST],
"ADMIN_COMMANDS" : [ADDPUP, ADDMEME, ADDMEOW, REMOVEPUP, REMOVEMEME, REMOVEMEOW, MUTE, UNMUTE],
}
# Create discord client
client = Bot(command_prefix=COMMAND_PREFIX)
client.remove_command('help')
# Don't make them wait when bot first launches
initial_ts=datetime.datetime.now() - datetime.timedelta(seconds=SPAM_THRESHOLD)
last_price = {}
last_meme = {}
last_pup = {}
last_meow = {}
def create_spam_dicts():
"""map every channel the client can see to datetime objects
this way we can have channel-specific spam prevention"""
global last_price
global last_meme
global last_pup
global last_meow
for c in client.get_all_channels():
if not is_private(c):
last_price[c.id] = initial_ts
last_meme[c.id] = initial_ts
last_pup[c.id] = initial_ts
last_meow[c.id] = initial_ts
@client.event
async def on_ready():
logger.info("Beatrice v%s started", BOT_VERSION)
logger.info("Discord.py API version %s", discord.__version__)
logger.info("Name: %s", client.user.name)
logger.info("ID: %s", client.user.id)
create_spam_dicts()
await client.change_presence(activity=discord.Game(settings.playing_status))
asyncio.get_event_loop().create_task(unsilence_users())
@client.event
async def on_member_join(member):
if db.silenced(member.id):
muzzled = discord.utils.get(member.guild.roles,name=settings.muzzled_role)
await member.add_roles(muzzled)
# Periodic check job to unsilence users
async def unsilence_users():
try:
await asyncio.sleep(10)
asyncio.get_event_loop().create_task(unsilence_users())
for s in db.get_silenced():
if s.expiration is None:
continue
elif datetime.datetime.now() >= s.expiration:
for guild in client.guilds:
if guild.id == s.server_id:
muzzled = discord.utils.get(guild.roles,name=settings.muzzled_role)
for member in guild.members:
if member.id == int(s.user_id):
await member.remove_roles(muzzled)
break
db.unsilence(s.user_id)
except Exception as ex:
logger.exception(ex)
def is_private(channel):
"""Check if a discord channel is private"""
return isinstance(channel, discord.abc.PrivateChannel)
def has_admin_role(roles):
"""Check if user has an admin role defined in our settings"""
for r in roles:
if r.name in settings.admin_roles:
return True
return False
def is_admin(user):
"""Returns true if user is an admin"""
if str(user.id) in settings.admin_ids:
return True
for m in client.get_all_members():
if m.id == user.id:
if has_admin_role(m.roles):
return True
return False
def valid_url(url):
# TODO we should check content-type header with aiohttp
return True
### Public Commands
@client.command()
async def commandlist(ctx):
message = ctx.message
embed = discord.Embed(colour=discord.Colour.magenta())
embed.title = "Commands"
for cmd in COMMANDS["USER_COMMANDS"]:
embed.add_field(name=cmd['CMD'], value=cmd['INFO'], inline=False)
if is_admin(message.author):
for cmd in COMMANDS["ADMIN_COMMANDS"]:
embed.add_field(name=cmd['CMD'], value=cmd['INFO'], inline=False)
await message.author.send(embed=embed)
@client.command()
async def price(ctx):
message = ctx.message
if is_private(message.channel):
return
# Check spam
global last_price
if message.channel.id not in last_price:
last_price[message.channel.id] = datetime.datetime.now()
tdelta = datetime.datetime.now() - last_price[message.channel.id]
if SPAM_THRESHOLD > tdelta.seconds:
await message.author.send("No more price for {0} seconds".format(SPAM_THRESHOLD - tdelta.seconds))
return
last_price[message.channel.id] = datetime.datetime.now()
msg = await message.channel.send("Retrieving latest prices...")
embed = discord.Embed(colour=discord.Colour.green())
embed.title = "Current Prices"
prices = await api.get_all_prices()
pricestr = "{0:.8f} BTC"
for exchange, price in prices:
embed.add_field(name=exchange, value=pricestr.format(price))
cmc = await api.get_cmc_data()
embed.description = ''
if cmc is not None:
embed.description += '**NANO**\n'
embed.description += cmc
banpernan, banperbtc, usdprice, volume, btcvolume = await api.get_banano_price()
mcap = 3402832669 * usdprice
banrank = await api.get_banano_rank(mcap, 1000)
if banpernan is not None:
embed.add_field(name='BANANO-NANO', value='{0:.2f} BAN : 1 NANO'.format(banpernan))
embed.add_field(name='BANANO-BTC', value='{0:.8f} BTC'.format(banperbtc))
embed.add_field(name='BAN-NANO 24H Vol.', value='{0:.6f} NANO'.format(volume))
embed.add_field(name='BAN-BTC 24H Vol.', value='{0:.4f} BTC'.format(btcvolume))
embed.add_field(name='BANANO USD', value='${0:.4f} : 1 BAN'.format(usdprice))
embed.add_field(name='BANANO RANK', value='#{0}'.format(banrank))
btc_usd = await api.get_btc_usd()
if btc_usd is not None:
embed.set_footer(text='BANANO Market Cap: ${1:,.2f} | 1 BTC = {0}'.format(btc_usd, mcap))
await msg.edit(content="", embed=embed)
@client.command()
async def meme(ctx):
message = ctx.message
if is_private(message.channel):
return
elif message.channel.id in settings.no_spam_channels:
return
# Check spam
global last_meme
if message.channel.id not in last_meme:
last_meme[message.channel.id] = datetime.datetime.now()
tdelta = datetime.datetime.now() - last_meme[message.channel.id]
if SPAM_THRESHOLD > tdelta.seconds:
await post_response(message, "No more memes for {0} seconds", (SPAM_THRESHOLD - tdelta.seconds))
return
last_meme[message.channel.id] = datetime.datetime.now()
meme = db.get_next_meme()
if meme is None:
await post_response(message, "There are no memes! Add some with !addmeme")
return
embed = discord.Embed(colour=discord.Colour.green())
embed.title = "Meme #{0} - {1}".format(meme['id'], meme['title'])
embed.set_author(name=meme['author'])
embed.set_image(url=meme['url'])
await message.channel.send(embed=embed)
@client.command(aliases=["memes"])
async def memelist(ctx):
message = ctx.message
memes = db.get_memes()
if len(memes) == 0:
embed = discord.Embed(colour=discord.Colour.red())
embed.title="No Memes"
embed.description="There no memes. Add memes with `{0}addmeme`".format(COMMAND_PREFIX)
await message.author.send(embed=embed)
return
title="Meme List"
description=("Here are all the memes!")
entries = []
for meme in memes:
entries.append(paginator.Entry(str(meme['id']),meme['url']))
# Do paginator for favorites > 10
if len(entries) > 10:
pages = paginator.Paginator.format_pages(entries=entries,title=title,description=description)
p = paginator.Paginator(client,message=message,page_list=pages,as_dm=True)
await p.paginate(start_page=1)
else:
embed = discord.Embed(colour=discord.Colour.teal())
embed.title = title
embed.description = description
for e in entries:
embed.add_field(name=e.name,value=e.value,inline=False)
await message.author.send(embed=embed)
@client.command()
async def meow(ctx):
message = ctx.message
if is_private(message.channel):
return
elif message.channel.id in settings.no_spam_channels:
return
# Check spam
global last_meow
if message.channel.id not in last_meow:
last_meow[message.channel.id] = datetime.datetime.now()
tdelta = datetime.datetime.now() - last_meow[message.channel.id]
if SPAM_THRESHOLD > tdelta.seconds:
await post_response(message, "No more meows for {0} seconds", (SPAM_THRESHOLD - tdelta.seconds))
return
last_meow[message.channel.id] = datetime.datetime.now()
meow = db.get_next_meow()
if meow is None:
await post_response(message, "There are no meows! Add some with !addmeow")
return
embed = discord.Embed(colour=discord.Colour.orange())
embed.title = "Meow #{0} - {1}".format(meow['id'], meow['title'])
embed.set_author(name=meow['author'])
embed.set_image(url=meow['url'])
await message.channel.send(embed=embed)
@client.command(aliases=["meows"])
async def meowlist(ctx):
message = ctx.message
meows = db.get_meows()
if len(meows) == 0:
embed = discord.Embed(colour=discord.Colour.red())
embed.title="No Meows"
embed.description="There no meows. Add meows with `{0}addmeow`".format(COMMAND_PREFIX)
await message.author.send(embed=embed)
return
title="Meow List"
description=("Here are all the meows!")
entries = []
for meow in meows:
entries.append(paginator.Entry(str(meow['id']),meow['url']))
# Do paginator for favorites > 10
if len(entries) > 10:
pages = paginator.Paginator.format_pages(entries=entries,title=title,description=description)
p = paginator.Paginator(client,message=message,page_list=pages,as_dm=True)
await p.paginate(start_page=1)
else:
embed = discord.Embed(colour=discord.Colour.teal())
embed.title = title
embed.description = description
for e in entries:
embed.add_field(name=e.name,value=e.value,inline=False)
await message.author.send(embed=embed)
@client.command()
async def pup(ctx):
message = ctx.message
if is_private(message.channel):
return
elif message.channel.id in settings.no_spam_channels:
return
# Check spam
global last_pup
if message.channel.id not in last_pup:
last_pup[message.channel.id] = datetime.datetime.now()
tdelta = datetime.datetime.now() - last_pup[message.channel.id]
if SPAM_THRESHOLD > tdelta.seconds:
await post_response(message, "No more pups for {0} seconds", (SPAM_THRESHOLD - tdelta.seconds))
return
last_pup[message.channel.id] = datetime.datetime.now()
pup = db.get_next_pup()
if pup is None:
await post_response(message, "There are no pups! Add some with !addpup")
return
embed = discord.Embed(colour=discord.Colour.blue())
embed.title = "Pup #{0} - {1}".format(pup['id'], pup['title'])
embed.set_author(name=pup['author'])
embed.set_image(url=pup['url'])
await message.channel.send(embed=embed)
@client.command(aliases=["pups"])
async def puplist(ctx):
message = ctx.message
pups = db.get_pups()
if len(pups) == 0:
embed = discord.Embed(colour=discord.Colour.red())
embed.title="No Pups"
embed.description="There no pups. Add pups with `{0}addpup`".format(COMMAND_PREFIX)
await message.author.send(embed=embed)
return
title="Pup List"
description=("Here are all the pups!")
entries = []
for pup in pups:
entries.append(paginator.Entry(str(pup['id']),pup['url']))
# Do paginator for favorites > 10
if len(entries) > 10:
pages = paginator.Paginator.format_pages(entries=entries,title=title,description=description)
p = paginator.Paginator(client,message=message,page_list=pages,as_dm=True)
await p.paginate(start_page=1)
else:
embed = discord.Embed(colour=discord.Colour.teal())
embed.title = title
embed.description = description
for e in entries:
embed.add_field(name=e.name,value=e.value,inline=False)
await message.author.send(embed=embed)
### Admin Commands
@client.command()
async def addmeme(ctx, url: str = None, author: str = None, title: str = None):
message = ctx.message
if not is_admin(message.author):
return
elif url is None or author is None or title is None:
await post_usage(message, ADDMEME)
elif not valid_url(url):
await message.author.send("Invalid URL. Valid urls begin with http:// or https://")
elif db.add_meme(url, author, title):
await message.author.send("Meme added: {0}".format(title))
else:
await message.author.send("Could not add meme {0}. It may already exist".format(url))
@client.command()
async def removememe(ctx, id: str):
message = ctx.message
if not is_admin(message.author):
return
elif db.remove_meme(id):
await message.author.send("Meme {0} removed".format(id))
else:
await message.author.send("Could not remove meme {0}. It may not exist".format(id))
@client.command()
async def addpup(ctx, url: str = None, author: str = None, title: str = None):
message = ctx.message
if not is_admin(message.author):
return
elif url is None or author is None or title is None:
await post_usage(message, ADDPUP)
elif not valid_url(url):
await message.author.send("Invalid URL. Valid urls begin with http:// or https://")
elif db.add_pup(url, author, title):
await message.author.send("Pup added: {0}".format(title))
else:
await message.author.send("Could not add Pup {0}. It may already exist".format(url))
@client.command()
async def removepup(ctx, id: str):
message = ctx.message
if not is_admin(message.author):
return
elif db.remove_pup(id):
await message.author.send("Pup {0} removed".format(id))
else:
await message.author.send("Could not Remove pup {0}. It may not exist".format(id))
@client.command()
async def addmeow(ctx, url: str = None, author: str = None, title: str = None):
message = ctx.message
if not is_admin(message.author):
return
elif url is None or author is None or title is None:
await post_usage(message, ADDMEOW)
elif not valid_url(url):
await message.author.send("Invalid URL. Valid urls begin with http:// or https://")
elif db.add_meow(url, author, title):
await message.author.send("Meow added: {0}".format(title))
else:
await message.author.send("Could not add meow {0}. It may already exist".format(url))
@client.command()
async def removemeow(ctx, id: str):
message = ctx.message
if not is_admin(message.author):
return
elif db.remove_meow(id):
await message.author.send("Meow {0} removed".format(id))
else:
await message.author.send("Could not remove meow {0}. It may not exist".format(id))
@client.command(aliases=['muzzle'])
async def mute(ctx):
message = ctx.message
if is_admin(message.author):
if len(message.mentions) > 0:
muzzled = discord.utils.get(message.guild.roles,name=settings.muzzled_role)
duration = find_amount(message.content)
expiration = None
if duration is not None:
expiration = datetime.datetime.now() + datetime.timedelta(minutes=float(duration))
for member in message.mentions:
if not db.silence(member.id, message.guild.id, expiration=expiration):
await post_response(message, '<@{0}> is already muzzled', member.id)
continue
await member.add_roles(muzzled)
if duration is not None:
await post_response(message, '<@{0}> has been muzzled for {1} minutes', member.id, duration)
else:
await post_response(message, '<@{0}> has been muzzled indefinitely', member.id)
await message.add_reaction('\U0001f694')
@client.command(aliases=['unmuzzle'])
async def unmute(ctx):
message = ctx.message
if is_admin(message.author):
if len(message.mentions) > 0:
muzzled = discord.utils.get(message.guild.roles,name=settings.muzzled_role)
for member in message.mentions:
await member.remove_roles(muzzled)
if not db.unsilence(member.id):
await post_response(message, '<@{0}> is not muzzled', member.id)
continue
await post_response(message, '<@{0}> has been unmuzzled', member.id)
@client.command()
async def arrest(ctx):
message = ctx.message
if is_admin(message.author):
if len(message.mentions) > 0:
jail = discord.utils.get(message.guild.roles,name=settings.ARREST_ROLE)
for member in message.mentions:
await member.add_roles(jail)
await post_response(message, settings.RIGHTS, mention_id=member.id)
await message.add_reaction('\U0001f694')
@client.command()
async def release(ctx):
message = ctx.message
if is_admin(message.author):
if len(message.mentions) > 0:
# Tip unban too
jail = discord.utils.get(message.guild.roles,name=settings.ARREST_ROLE)
for member in message.mentions:
await member.remove_roles(jail)
await post_response(message, settings.RELEASE, mention_id=member.id)
@client.command()
async def troll(ctx):
message = ctx.message
if is_admin(message.author):
if len(message.mentions) > 0:
troll = discord.utils.get(message.guild.roles,name=settings.TROLL_ROLL)
citizenship = discord.utils.get(message.guild.roles,name=settings.CITIZEN_ROLE)
for member in message.mentions:
await member.add_roles(troll)
await member.remove_roles(citizenship)
await post_response(message, settings.TROLL, mention_id=member.id)
@client.command()
async def untroll(ctx):
message = ctx.message
if is_admin(message.author):
if len(message.mentions) > 0:
troll = discord.utils.get(message.guild.roles,name=settings.TROLL_ROLL)
for member in message.mentions:
await member.remove_roles(troll)
await post_response(message, settings.UNTROLL, mention_id=member.id)
@client.command()
async def citizenship(ctx):
message = ctx.message
if is_admin(message.author):
if len(message.mentions) > 0:
citizenship = discord.utils.get(message.guild.roles,name=settings.CITIZEN_ROLE)
for member in message.mentions:
await member.add_roles(citizenship)
await post_response(message, settings.CITIZENSHIP, mention_id=member.id)
await message.add_reaction('\:bananorepublic:429691019538202624')
@client.command()
async def deport(ctx):
message = ctx.message
if is_admin(message.author):
if len(message.mentions) > 0:
citizenship = discord.utils.get(message.guild.roles,name=settings.CITIZEN_ROLE)
for member in message.mentions:
await member.remove_roles(citizenship)
await post_response(message, settings.DEPORT, mention_id=member.id)
await message.add_reaction('\U0001F6F3')
### Re-Used Discord Functions
async def post_response(message, template, *args, mention_id=None):
if mention_id is None:
mention_id = message.author.id
response = template.format(*args)
if not is_private(message.channel):
response = "<@" + str(mention_id) + "> \n" + response
logger.info("sending response: '%s' for message: '%s' to userid: '%s' name: '%s'", response, message.content, message.author.id, message.author.name)
asyncio.sleep(0.05) # Slight delay to avoid discord bot responding above commands
return await message.channel.send(response)
async def post_usage(message, command):
embed = discord.Embed(colour=discord.Colour.purple())
embed.title = "Usage:"
embed.add_field(name=command['CMD'], value=command['USAGE'],inline=False)
await message.author.send(embed=embed)
#
def find_amount(input_text):
regex = r'(?:^|\s)(\d*\.?\d+)(?=$|\s)'
matches = re.findall(regex, input_text, re.IGNORECASE)
if len(matches) >= 1:
return float(matches[0].strip())
else:
return None
# Start the bot
client.run(settings.discord_bot_token)