-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
1050 lines (847 loc) · 29 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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Discord bot for the UWS Game Dev Society, originally developed by Martin Grant"""
##### Created 28/02/2018
##### Last Update 18/09/2018
##### Version 0.2
##### Contributors
#
# https://github.com/medallyon
#
#####
# import any applicable env_vars from '/.env'
from dotenv import load_dotenv
load_dotenv()
import os
from pathlib import Path
import random as rand
import sys
import re
from datetime import datetime, timedelta
from time import time as timestamp
import json
import math as pythonmath
import asyncio
from decimal import Decimal
import discord
from discord.ext import commands
from discord.utils import get
import requests
#import wikipedia
from translate import Translator
from coinmarketcap import Market
from forex_python.converter import CurrencyRates
import wolframalpha
import utilities as utils
# import modules.weather
import modules.hangman
REPOSITORY_URL = "https://github.com/martygrant/uwsgamedevbot"
VERSION_NUMBER = os.getenv("VERSION")
BOT_TOKEN = os.getenv("BOT_TOKEN")
GIPHY_TOKEN = os.getenv("GIPHY_TOKEN")
WOLFRAM_KEY = os.getenv("WOLFRAM_TOKEN")
DEVELOPERS = [ 129036763367735297, 317284059275460608 ]
true = True
false = False
##### [ CLASSES ] #####
class SavableDict(dict):
"""An extended dict that allows saving to file"""
@property
def raw_dict(self):
"""Returns a readable representation of this dict"""
target = {}
for key, val in self.items():
if not str(key).startswith('_'):
target[key] = val
return target
def __init__(self, destination, **kwarg):
super().__init__(kwarg)
self._dest = destination
# Create a file handle for this file
this_dict = {}
self._file_handle = Path(self._dest)
# Create this file if it does not exist
if not self._file_handle.is_file():
self._file_handle.write_text(json.dumps(this_dict))
# Or load this file and load it into this object
else:
this_dict = json.loads(self._file_handle.read_text())
for key, val in this_dict.items():
self[key] = val
def __setitem__(self, key, val):
super().__setitem__(key, val)
self.save()
def __delitem__(self, key):
super().__delitem__(key)
self.save()
def save(self):
"""Saves the dict to file"""
encoded_json = json.dumps(self.raw_dict, indent=2)
self._file_handle.write_text(encoded_json)
class OngoingPolls(SavableDict):
"""The class responsible for poll persistence"""
@property
def raw_dict(self):
target_dict = {}
for key, value in self.items():
if not str(key).startswith('_') and isinstance(value, Poll):
target_dict[key] = value.raw_dict
return target_dict
def __init__(self):
super().__init__("persistence/ongoing_polls.json")
async def reinitialise(self):
"""Re-instantiates the polls saved to file"""
decoded = {}
with open(self._dest) as file:
decoded = json.loads(file.read())
for key, val in decoded.items():
if val["timestamp"] + val["duration"] <= timestamp():
continue
restored_poll = await Poll.restore(val)
if restored_poll is not None:
self[key] = restored_poll
class Poll:
"""An instance of this class handles the creation of a poll and monitoring of reactions"""
@staticmethod
async def restore(poll_dict):
"""Restores a poll from a given dictionary"""
question_message = None
owner = None
try:
question_message = await BOT.get_message(poll_dict["channel_id"], poll_dict["message_id"])
owner = await BOT.get_user_info(poll_dict["owner_id"])
except discord.NotFound:
# Message or Owner don't exist or cannot be found: skip instancing poll
return None
options = []
results = {}
for letter, result in poll_dict["results"].items():
options.append(result["option_name"])
results[letter] = result["votes"]
new_poll = Poll(poll_dict["question"], options, poll_dict["timestamp"], poll_dict["duration"] - (timestamp() - poll_dict["timestamp"]), owner, question_message)
new_poll.results = results
await new_poll.start()
return new_poll
@property
def raw_dict(self):
"""Returns the raw dict representation of this instance"""
results = {}
for i, letter in enumerate(self.results):
results[letter] = {
"option_name": self.options[i],
"votes": self.results[letter]
}
return {
"message_id": self.question_message.id,
"timestamp": self.timestamp,
"duration": self.duration,
"question": self.question,
"owner_id": self.initiator.id,
"channel_id": self.channel.id,
"results": results
}
@property
def time_left(self):
"""Gets the time left for this instance of the poll (in milliseconds)"""
return self.time_to_stop - timestamp()
@property
def embed(self):
"""Constructs a Rich Embed for this poll"""
e = discord.Embed(type="rich",
description="This is {}'s poll.".format(self.initiator.mention),
timestamp=datetime.now() + timedelta(seconds=self.duration),
colour=utils.generate_random_colour())
e.add_field(name="\u200b", value="**Options**", inline=False)
for i, option in enumerate(self.options):
current_votes = len(self.results[utils.ALPHABET[i]])
e.add_field(name="{}. {}".format(utils.ALPHABET[i].upper(), option), value="{} votes".format(current_votes), inline=True)
e.set_author(name=self.question,
icon_url=self.initiator.avatar_url)
if self.time_left > 0:
e.set_footer(text="Time remaining: {} seconds".format(int(self.time_left)))
return e
def __init__(self, question, options, ts, duration, owner, message=None):
self.destroyed = False
self.question = question
self.question_message = None
self.options = options
self.timestamp = ts
self.duration = duration
self.time_to_stop = self.timestamp + duration
self.initiator = owner
self.results = {}
for i in range(len(self.options)):
self.results[utils.ALPHABET[i]] = []
# If a 'message' object already exists, use that
if isinstance(message, discord.Message):
self.channel = message.channel
self.question_message = message
# If not, create a new message in 'Poll.start()'
elif isinstance(message, discord.Channel):
self.channel = message
else:
raise TypeError("The \{message\} argument must be of type 'Discord.Message' or 'Discord.Channel'")
def add_vote(self, option, user):
"""Adds a vote to the current voting pool"""
user_id = user
if isinstance(user, discord.User):
user_id = user.id
self.results[option].append(user_id)
BOT.ongoing_polls.save()
def remove_vote(self, option, user):
"""Removes a vote from the current voting pool"""
user_id = user
if isinstance(user, discord.User):
user_id = user.id
self.results[option].remove(user_id)
BOT.ongoing_polls.save()
async def start(self):
"""Starts the poll and creates a task for the poll to end using by the duration"""
# Send a new message if one wasn't already passed to the constructor
if self.question_message is None:
self.question_message = await self.channel.send(embed=self.embed)
BOT.ongoing_polls[self.question_message.id] = self
if self.question_message not in BOT.messages:
BOT.messages.append(self.question_message)
await self.add_reactions()
BOT.loop.create_task(self.stop())
async def stop(self):
"""Stops the poll and posts the results"""
await BOT.wait_until_ready()
await asyncio.sleep(self.duration)
if self.destroyed:
return
# TODO: finalise data to prettify output
await self.channel.send("**{}**'s poll has finished. Here are the results.".format(self.initiator.mention), embed=self.embed)
# BOT.ongoing_polls.pop(self.question_message.id, self)
del BOT.ongoing_polls[self.question_message.id]
def destroy(self):
self.destroyed = True
async def add_reactions(self):
"""Adds the respective reactions for each option for users to react to"""
for i in range(len(self.options)):
await BOT.add_reaction(self.question_message, utils.resolve_emoji_from_alphabet(utils.ALPHABET[i].lower()))
class CustomBot(commands.Bot):
"""An extension of the discord.py Bot Class"""
def __init__(self, command_prefix, home_server_id="", formatter=None, description=None, pm_help=False, **options):
"""Custom constructor that creates some custom attributes"""
super().__init__(command_prefix, formatter=formatter, description=description, pm_help=pm_help, options=options)
self.home_server_id = home_server_id
self.home_server = None
self.ongoing_polls = OngoingPolls()
lastBjarneChoice = -1
last8BallChoice = -1
##### [ BOT INSTANTIATION ] #####
BOT = CustomBot(description="Below is a listing for Bjarne's commands. Use '!' infront of any of them to execute a command, like '!help'", command_prefix="!", home_server_id="405451738804518916")
BOT.load_extension('modules.hangman')
BOT.load_extension('modules.dictionary')
BOT.load_extension('modules.gameDevDictionary')
##### [ EVENT LISTENERS ] #####
@BOT.event
async def on_ready():
"""The 'on_ready' event"""
print("Logged in as {} ({})\n------".format(BOT.user.name, BOT.user.id))
await BOT.ongoing_polls.reinitialise()
@BOT.event
async def on_member_join(member):
"""The 'on_member_join' event"""
welcome_message = """Welcome to the **UWS Game Development Society**! Please review the rules in the <#579342050156347392> channel. Otherwise, type `!help` for a list of my commands."""
# Send the welcome message to the user individually
await member.send(welcome_message)
@BOT.event
async def on_message_delete(message):
"""The 'on_message_deleted' event"""
if message.id not in BOT.ongoing_polls:
return
deleted_poll = BOT.ongoing_polls[message.id]
deleted_poll.destroy()
await deleted_poll.initiator.send("Your poll with the question `{}` in {} was deleted. Here are the results.".format(deleted_poll.question, deleted_poll.channel.mention), embed=deleted_poll.embed)
@BOT.event
async def on_reaction_add(reaction, user):
"""The 'on_reaction_add' event"""
if user.id == BOT.user.id:
return
if reaction.message.id in BOT.ongoing_polls:
current_poll = BOT.ongoing_polls[reaction.message.id]
# If the reaction is the same as any of the existing reactions
if any(utils.resolve_emoji_from_alphabet(option) == reaction.emoji for option in current_poll.results.keys()):
# Add the user to the respective result object
selected_option = utils.resolve_letter_from_emoji(reaction.emoji)
if user.id not in current_poll.results[selected_option]:
current_poll.add_vote(selected_option, user)
# Update the original poll message
return await BOT.edit_message(current_poll.question_message, embed=current_poll.embed)
@BOT.event
async def on_reaction_remove(reaction, user):
"""The 'on_reaction_remove' event"""
if user.id == BOT.user.id:
return
if reaction.message.id in BOT.ongoing_polls:
current_poll = BOT.ongoing_polls[reaction.message.id]
# If the reaction is the same as any of the existing reactions
if any(utils.resolve_emoji_from_alphabet(option) == reaction.emoji for option in current_poll.results.keys()):
# Remove the user from the respective result object
deselected_option = utils.resolve_letter_from_emoji(reaction.emoji)
if user.id in current_poll.results[deselected_option]:
current_poll.remove_vote(deselected_option, user)
# Update the original poll message
return await BOT.edit_message(current_poll.question_message, embed=current_poll.embed)
DANK_MESSAGE_MAP = [
["ayy", "lmao"],
["rip", "Press F to pay respects\n`F`"],
["vape", "<:vapenation:423973451716624391>"],
[["putin", "soviet", "lenin", "stalin"], "<:soviet:423927402637295617>"]
]
@BOT.event
async def on_message(message):
"""The 'on_message' event handler"""
# Fetch home_server if not existent
if not BOT.home_server and message.guild and BOT.home_server_id == message.guild.id:
BOT.home_server = message.guild
if message.author.id == BOT.user.id:
return
# HANGMAN letter listener
if message.channel.id in BOT.hangman_games and len(message.content) == 1:
await BOT.hangman_games[message.channel.id].process_message(message)
await BOT.process_commands(message)
# Dank Messages - check per word in message
if message.content and message.content[0] != BOT.command_prefix:
split_message = message.content.lower().split(' ')
prog = re.compile("^<:\D+:(\d+)>$")
# Capsule this into its own function, code re-use
async def post_message_or_reaction(val):
# if key is an emoji string
emoji_match = prog.match(val)
if emoji_match:
if BOT.home_server:
# Get the emoji ID from REGEXP match
emoji_id = emoji_match[1]
emoji = next((e for e in BOT.home_server.emojis if e.id == emoji_id), None)
if emoji:
await BOT.add_reaction(message, emoji)
else:
await message.channel.send(val)
else:
await message.channel.send(val)
else:
await message.channel.send(val)
for dankness in DANK_MESSAGE_MAP:
key = dankness[0]
val = dankness[1]
if isinstance(key, list):
if any(x in split_message for x in key):
await post_message_or_reaction(val)
break
else:
if key in split_message:
await post_message_or_reaction(val)
break
##### [ BOT COMMANDS ] #####
@BOT.command()
async def evaluate(ctx, *expression):
"""Evaluate an expression."""
if ctx.message.author.id not in DEVELOPERS:
return await ctx.message.channel.send("Wait, you're not a developer. Don't try anything.")
result = None
try:
result = eval(' '.join(expression))
except Exception as error:
result = error
await ctx.message.channel.send(result)
@BOT.command()
async def say(ctx, *something):
"""Make Bjarne say something."""
if something:
await ctx.message.channel.send(" ".join(something))
@BOT.command()
async def version(ctx):
"""Display Bjarne version info."""
await ctx.message.channel.send("v{} - {}".format(VERSION_NUMBER, REPOSITORY_URL))
@BOT.command()
async def bjarnequote(ctx):
"""Get a quote from Bjarne Stroustrup, creator of C++."""
quotes = [
'A program that has not been tested does not work.',
'An organisation that treats its programmers as morons will soon have programmers that are willing and able to act like morons only.',
'Anybody who comes to you and says he has a perfect language is either naïve or a salesman.',
'C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.',
'The standard library saves programmers from having to reinvent the wheel.',
'Certainly not every good program is object-oriented, and not every object-oriented program is good.',
'Clearly, I reject the view that there is one way that is right for everyone and for every problem.',
'Thus, the standard library will serve as both a tool and as a teacher.',
'There are only two kinds of languages: the ones people complain about and the ones nobody uses.',
'I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone.',
'If you think it\'s simple, then you have misunderstood the problem.',
'C++ is designed to allow you to express ideas, but if you don\'t have ideas or don\'t have any clue about how to express them, C++ doesn\'t offer much help.',
'Programming is like sex: It may give some concrete results, but that is not why we do it.',
]
choice = rand.choice(quotes)
# so we don't get the same quote twice in a row
while choice == BOT.lastBjarneChoice:
choice = rand.choice(quotes)
BOT.lastBjarneChoice = choice
await ctx.message.channel.send(choice)
@BOT.command()
async def random(ctx, *arg):
"""Generate a random number. Use '!help random' for usage.
!random for any random number.
!random x for between 0 and x.
!random x y for between 0 and y.
"""
random_number = -1
# If no argument passed, get any random number
if not arg:
random_number = rand.randint(0, sys.maxsize)
else:
# If we have 1 argument, get a number between 0 and x
if len(arg) == 1:
x = int(arg[0])
random_number = rand.randint(0, x)
else:
# If we have 2 arguments, get a number between them
x = int(arg[0])
y = int(arg[1])
random_number = rand.randint(x, y)
await ctx.message.channel.send(random_number)
@BOT.command()
async def dice(ctx):
"""Roll a dice."""
await ctx.message.channel.send(rand.randint(1, 6))
# todo: use arguments, should make this command much simpler
@BOT.command()
async def math(*, arg):
"""Perform math operations, e.g '10 + 20'
Supports: (+ / * -)
sq x - square x
sqrt x - square root of x
pi - the constant pi
degrees - convert radians to degrees
radians - convert degrees to radians
ceiling - get ceiling of a float, e.g ceil 7.7 = 8
floor - get floor of a float, e.g. floor 7.7 = 7
"""
arg = arg.split()
z = "Error."
if arg[0] == "sq":
x = float(arg[1])
z = float(x * x)
elif arg[0] == "sqrt":
x = float(arg[1])
z = float(pythonmath.sqrt(x))
elif arg[0] == "pi":
z = pythonmath.pi
elif arg[0] == "degrees":
x = float(arg[1])
z = pythonmath.degrees(x)
elif arg[0] == "radians":
x = float(arg[1])
z = pythonmath.radians(x)
elif arg[0] == "ceiling":
x = float(arg[1])
z = pythonmath.ceil(x)
elif arg[0] == "floor":
x = float(arg[1])
z = pythonmath.floor(x)
else:
operator = arg[1]
x = float(arg[0])
y = float(arg[2])
if operator == "+":
z = x + y
if operator == "/":
if y == 0:
z = "DENIED."
else:
z = x / y
if operator == "*":
z = x * y
if operator == "-":
z = x - y
if z != "DENIED.":
# Strip trailing 0s if we just have a whole number result
z = '%g' % (Decimal(str(z)))
await ctx.message.channel.send(z)
@BOT.command()
async def quote(ctx, *arg):
"""Quote a user randomly. Usage: !quote <username>, if no user is specified it will quote yourself."""
channel = ctx.message.channel
messages = []
user = ctx.message.author.nick
if arg:
user = arg[0]
user = ""
# If username has spaces, we need to build a string for it and remove the last space
for x in arg:
user += x
user += " "
user = user[:-1]
user = user.lower()
async for message in BOT.logs_from(channel, limit=2000):
if channel.is_private == False:
if message.author.display_name.lower() == user:
messages.append(message.content)
else:
if message.author.nick.lower() == user:
messages.append(message.content)
# Pick a random message and output it
random_message = messages[rand.randint(0, len(messages))]
await ctx.message.channel.send("{} once said: `{}`".format(user, random_message))
@BOT.command()
async def poll(ctx):
"""Starts a new poll. Usage: !poll -Question -durationInSeconds -Option -Option -Option..."""
args = ctx.message.content.split("-")
if len(args) < 2:
return await ctx.message.channel.send("Please see the command usage for this command: `{}poll -Question -durationInSeconds -Option -Option -Option...`".format(BOT.command_prefix))
question = args[1]
duration = args[2]
duration_float = float(duration)
if duration_float > 86400:
return await ctx.message.channel.send("Poll cannot last longer than one day (86400 seconds).")
args.pop(0)
args.pop(0)
args.pop(0)
# Create a list of options and trim any whitespace from the string
options = []
for option in args:
if option.strip() not in options:
options.append(option.strip())
# Create a new Poll instance and start it
new_poll = Poll(question, options, timestamp(), duration_float, ctx.message.author, ctx.message.channel)
await new_poll.start()
def getOnlineUserCount(users):
count = 0
for user in users:
if str(user.status) == "online" or str(user.status) == "idle" or str(user.status) == "dnd":
count += 1
return count
def getNewestMember(users):
userList = list(users)
newest = userList[0]
for x in userList[2:]:
if x.joined_at > newest.joined_at:
newest = x
return newest
@BOT.command()
async def stats(ctx):
"""Get server statistics."""
server = ctx.message.author.guild
serverName = server.name
numberOfUsers = server.member_count
members = server.members
numberOfOnlineUsers = getOnlineUserCount(members)
createdDate = server.created_at.strftime('%Y-%m-%d')
newestMember = getNewestMember(members)
embed = discord.Embed(type="rich", colour=utils.generate_random_colour(), timestamp=datetime.now())
embed.set_thumbnail(url=server.icon_url)
embed.set_author(name=serverName)
embed.add_field(name="Created", value=createdDate)
embed.add_field(name="Users Online", value=numberOfOnlineUsers)
embed.add_field(name="Users Total", value=numberOfUsers)
embed.add_field(name="Newest Member", value=newestMember)
await ctx.message.channel.send(embed=embed)
@BOT.command()
async def urban(ctx, query):
"""Search for a definition from Urban Dictionary."""
defineURL = 'https://api.urbandictionary.com/v0/define?term='
response = requests.get(defineURL + query)
data = response.json()
firstEntry = data["list"][0]
definition = firstEntry["definition"]
example = firstEntry["example"]
url = firstEntry["permalink"]
title = "Urban Dictionary: "
title += query
embed = discord.Embed(type="rich", colour=utils.generate_random_colour(), timestamp=datetime.now())
embed.set_author(name=title)
embed.add_field(name="Definition", value=definition)
embed.add_field(name="Example", value=example)
embed.add_field(name="URL", value=url)
await ctx.message.channel.send(embed=embed)
@BOT.command()
async def report(ctx, user):
"""Report a user anonymously to the society committee. Usage: !report <user> <reason>"""
reason = ctx.message.content
reason = reason.replace("!report " + user, "")
reason = reason[1:]
message = "A user has made a report against another user.\nThis is against: "
message += "`" + user + "` for the reason: `"
message += reason + "`."
await BOT.get_channel(416255534438547456).send(message)
@BOT.command()
async def eightball(ctx, *arg):
"""Let the magic 8 ball provide you with wisdom."""
if arg:
options = [
"It is certain. :+1:",
"It is decidedly so. :+1:",
"Without a doubt. :+1:",
"Yes - definitely. :+1:",
"You may rely on it. :+1:",
"As I see it, yes. :+1:",
"Most likely. :+1:",
"Outlook good. :+1:",
"Yes. :+1:",
"Signs point to yes. :+1:",
"Reply hazy, try again. :shrug:",
"Ask again later. :shrug:",
"Better not tell you now. :shrug:",
"Cannot predict now. :shrug:",
"Concentrate and ask again. :shrug:",
"Don't count on it. :shrug:",
"My reply is no. :-1:",
"My sources say no. :-1:",
"Outlook not so good. :-1:",
"Very doubtful. :-1:"
]
choice = rand.choice(options)
# so we don't get the same quote twice in a row
while choice == BOT.last8BallChoice:
choice = rand.choice(options)
BOT.last8BallChoice = choice
await ctx.message.channel.send(choice)
else:
await ctx.message.channel.send("You must ask a question!")
@BOT.command()
async def xkcd(ctx):
"""Get a random XKCD comic."""
choice = rand.randint(0, 2058)
url = 'http://xkcd.com/' + str(choice) + '/info.0.json'
response = requests.get(url)
data = response.json()
comic = data["img"]
await ctx.message.channel.send(comic)
@BOT.command()
async def wiki(ctx):
"""Get the first few sentences of a Wikipedia page."""
query = ctx.message.content
query = query.replace('!wiki', '')
summary = wikipedia.summary(query, auto_suggest=True, sentences=2)
page = wikipedia.page(query, auto_suggest=True)
title = "Wikipedia: "
title += page.title
URL = page.url
embed = discord.Embed(type="rich", colour=utils.generate_random_colour(), timestamp=datetime.now())
embed.set_author(name=title)
embed.add_field(name="Summary", value=summary)
embed.add_field(name="Read More", value=URL)
await ctx.message.channel.send(embed=embed)
@BOT.command()
async def translate(ctx):
"""Translate a message like '!translate en ja hello' to translate 'hello' from English to Japanese. See https://en.wikipedia.org/wiki/ISO_639-1 for language codes."""
query = ctx.message.content
query = query.split()
fromlang = query[1]
tolang = query[2]
message = ""
for x in query[3:]:
message += x
message += " "
translator = Translator(to_lang=tolang, from_lang=fromlang)
translation = translator.translate(message)
output = "`" + translation + "` <- "
output += message
output += " ("
output += fromlang
output += "-"
output += tolang
output += ")"
await ctx.message.channel.send(output)
def cryptoChange(val):
if (val > 0):
return " :arrow_up:"
else:
return " :arrow_down:"
@BOT.command()
async def crypto(ctx, *symbol):
"""Get info about crypto currencies. '!crypto btc' to get info about one specific currency."""
coins = Market()
listings = coins.ticker(start=0, limit=10, convert='GBP')
t = listings["data"]
coinOutputs = []
for x in t:
coin = listings["data"][x]
output = ""
output += coin["name"]
output += " ("
output += coin["symbol"]
output += ") £"
output += str(round(coin["quotes"]["GBP"]["price"], 3))
output += "\t1hr % "
hr = coin["quotes"]["GBP"]["percent_change_1h"]
output += str(hr)
output += cryptoChange(hr)
output += "\t24hr % "
hr = coin["quotes"]["GBP"]["percent_change_24h"]
output += str(hr)
output += cryptoChange(hr)
output += "\t7d % "
hr = coin["quotes"]["GBP"]["percent_change_7d"]
output += str(hr)
output += cryptoChange(hr)
output += "\n"
coinOutputs.append(output)
output = ""
for x in coinOutputs:
output += x
if symbol:
for x in coinOutputs:
if x.find(str(symbol[0].upper())) >= 0:
output = x
await ctx.message.channel.send(output)
@BOT.command()
async def convert(ctx, value: float, fromUnit, toUnit):
"""Convert between quantities.
Metre to Imperial: feet, mile, yard, inch.
Temperatures: c, f, k.
Time: secs, mins, hours, days.
Currency: Forex symbols e.g USD, GBP etc.
"""
message = "Conversion not yet supported. Use !help convert to see supported conversions."
result = value
valid = False
# Metre to Imperial Lengths
metreToImperial = {
"feet": 3.281,
"mile": 1609.344,
"yard": 1.094,
"inch": 39.37
}
if fromUnit == "m" and toUnit in metreToImperial:
result = value * metreToImperial[toUnit]
if fromUnit in metreToImperial and toUnit == "m":
result = value / metreToImperial[fromUnit]
# Temperatures
if fromUnit == "c":
if toUnit == "k":
result = value + 273.15
if toUnit == "f":
result = (value * 9/5) + 32
if fromUnit == "k":
if toUnit == "c":
result = value - 273.15
if toUnit == "f":
result = (value - 273.15) * (9/5) + 32
if fromUnit == "f":
if toUnit == "c":
result = (value - 32) * 5/9
if toUnit == "k":
result = (value + -32) * (5/9) + 273.15
# Time
if fromUnit == "secs":
if toUnit == "mins":
result = value / 60
if toUnit == "hours":
result = value / 3600
if toUnit == "days":
result = value / 86400
if fromUnit == "mins":
if toUnit == "secs":
result = value * 60
if toUnit == "hours":
result = value / 60
if toUnit == "days":
result = value / 1440
if fromUnit == "hours":
if toUnit == "secs":
result = value * 3600
if toUnit == "mins":
result = value * 60
if toUnit == "days":
result = value / 24
if fromUnit == "days":
if toUnit == "secs":
result = value * 86400
if toUnit == "mins":
result = value * 1440
if toUnit == "hours":
result = value * 24
# Currency
currency = False
c = CurrencyRates()
try:
currency = True
result = c.convert(fromUnit.upper(), toUnit.upper(), value)
message = "Conversion: " + str(value)
message += " "
message += fromUnit
message += " equals "
message += str(result)
message += " "
message += toUnit
except:
currency = False
pass
if result != value and currency == False:
message = "Conversion: " + str(value)
message += " "
message += fromUnit
message += " equals "
message += str(result)
message += " "
message += toUnit
await ctx.message.channel.send(message)
@BOT.command()
async def modules(ctx, course):
"""Display course module ratings. Options are: CGT, CGD."""
message = "Module ratings for: `"
message += course.upper()
message += "`\n"
course = course.lower()
with open('courseratings.json') as f:
data = json.load(f)
for x in data[course].items():
message += x[0]
message += ":\t `"
message += str(x[1][0])
message += "` ("
message += str(len(x[1][1])) + " votes)"
message += "\n"
await ctx.message.channel.send(message)
@BOT.command()
async def ratemodule(ctx, *arg):
"""Rate a course module at UWS. Specify command like '!ratecourse course rating module' e.g: '!ratecourse cgt 5 intro to programming'"""
message = ""
course = arg[0]
rating = float(arg[1])
module = ""
for x in arg[2:]:
module += x
module += " "
module = module[:-1]
print(course)
print(rating)
print(module)