2016-07-23 23:58:35 +12:00
|
|
|
import discord
|
2016-07-09 13:27:19 +12:00
|
|
|
from discord.ext import commands
|
2017-02-12 12:14:57 +13:00
|
|
|
|
|
|
|
from . import utils
|
2016-07-23 23:58:35 +12:00
|
|
|
|
2017-03-24 16:11:22 +13:00
|
|
|
from bs4 import BeautifulSoup as bs
|
2016-07-09 13:27:19 +12:00
|
|
|
import subprocess
|
2016-07-17 02:45:44 +12:00
|
|
|
import glob
|
2016-07-09 13:27:19 +12:00
|
|
|
import random
|
2016-07-11 01:58:46 +12:00
|
|
|
import re
|
2016-07-19 11:25:03 +12:00
|
|
|
import calendar
|
2016-08-07 10:20:16 +12:00
|
|
|
import pendulum
|
2016-07-19 11:25:03 +12:00
|
|
|
import datetime
|
2017-04-21 15:03:52 +12:00
|
|
|
import psutil
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2016-07-19 11:29:40 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
class Core:
|
2016-07-23 23:58:35 +12:00
|
|
|
"""Core commands, these are the miscallaneous commands that don't fit into other categories'"""
|
2016-07-11 09:18:30 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2016-12-02 16:58:28 +13:00
|
|
|
# This is a dictionary used to hold information about which page a certain help message is on
|
|
|
|
self.help_embeds = {}
|
|
|
|
self.results_per_page = 10
|
|
|
|
self.commands = None
|
2017-04-21 15:03:52 +12:00
|
|
|
self.process = psutil.Process()
|
|
|
|
self.process.cpu_percent()
|
2016-12-02 16:58:28 +13:00
|
|
|
|
2017-03-06 15:45:44 +13:00
|
|
|
@commands.command()
|
2017-02-12 12:14:57 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-11-29 21:05:22 +13:00
|
|
|
async def help(self, ctx, *, message=None):
|
2016-12-20 05:31:45 +13:00
|
|
|
"""This command is used to provide a link to the help URL.
|
|
|
|
This can be called on a command to provide more information about that command
|
|
|
|
You can also provide a page number to pull up that page instead of the first page
|
|
|
|
|
|
|
|
EXAMPLE: !help help
|
|
|
|
RESULT: This information"""
|
2016-11-29 16:51:18 +13:00
|
|
|
|
2016-12-20 05:41:33 +13:00
|
|
|
cmd = None
|
2016-12-20 05:42:23 +13:00
|
|
|
page = 1
|
2017-01-25 09:45:17 +13:00
|
|
|
|
|
|
|
if message is not None:
|
2016-12-20 05:31:45 +13:00
|
|
|
# If something is provided, it can either be the page number or a command
|
|
|
|
# Try to convert to an int (the page number), if not, then a command should have been provided
|
|
|
|
try:
|
|
|
|
page = int(message)
|
|
|
|
except:
|
2017-03-06 15:45:44 +13:00
|
|
|
cmd = self.bot.get_command(message)
|
2016-11-29 16:51:18 +13:00
|
|
|
|
|
|
|
if cmd is None:
|
2017-03-27 16:13:36 +13:00
|
|
|
entries = []
|
|
|
|
for cmd in utils.get_all_commands(self.bot):
|
|
|
|
if await cmd.can_run(ctx):
|
|
|
|
entries.append(cmd.qualified_name)
|
2017-02-13 12:36:11 +13:00
|
|
|
try:
|
2017-02-13 12:58:27 +13:00
|
|
|
pages = utils.Pages(self.bot, message=ctx.message, entries=entries)
|
2017-02-18 13:00:34 +13:00
|
|
|
await pages.paginate(start_page=page)
|
2017-02-13 12:36:11 +13:00
|
|
|
except utils.CannotPaginate as e:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send(str(e))
|
2016-11-29 16:51:18 +13:00
|
|
|
else:
|
2016-12-20 05:52:00 +13:00
|
|
|
# Get the description for a command
|
2016-11-29 16:51:18 +13:00
|
|
|
description = cmd.help
|
2017-01-24 09:28:11 +13:00
|
|
|
if description is not None:
|
|
|
|
# Split into examples, results, and the description itself based on the string
|
|
|
|
example = [x.replace('EXAMPLE: ', '') for x in description.split('\n') if 'EXAMPLE:' in x]
|
|
|
|
result = [x.replace('RESULT: ', '') for x in description.split('\n') if 'RESULT:' in x]
|
|
|
|
description = [x for x in description.split('\n') if x and 'EXAMPLE:' not in x and 'RESULT:' not in x]
|
|
|
|
else:
|
|
|
|
example = None
|
|
|
|
result = None
|
2016-12-20 05:52:00 +13:00
|
|
|
# Also get the subcommands for this command, if they exist
|
2017-03-26 14:27:10 +13:00
|
|
|
subcommands = [x.qualified_name for x in utils.get_all_subcommands(cmd) if x.qualified_name != cmd.qualified_name]
|
2016-11-29 16:51:18 +13:00
|
|
|
|
2016-12-20 05:52:00 +13:00
|
|
|
# The rest is simple, create the embed, set the thumbail to me, add all fields if they exist
|
2016-11-29 16:51:18 +13:00
|
|
|
embed = discord.Embed(title=cmd.qualified_name)
|
2017-01-22 15:35:20 +13:00
|
|
|
embed.set_thumbnail(url=self.bot.user.avatar_url)
|
2017-01-24 09:28:11 +13:00
|
|
|
if description:
|
|
|
|
embed.add_field(name="Description", value="\n".join(description), inline=False)
|
2016-11-29 16:51:18 +13:00
|
|
|
if example:
|
2016-11-29 17:55:55 +13:00
|
|
|
embed.add_field(name="Example", value="\n".join(example), inline=False)
|
2016-11-29 16:51:18 +13:00
|
|
|
if result:
|
2016-11-29 17:55:55 +13:00
|
|
|
embed.add_field(name="Result", value="\n".join(result), inline=False)
|
2016-12-20 05:53:25 +13:00
|
|
|
if subcommands:
|
2016-12-20 05:52:36 +13:00
|
|
|
embed.add_field(name='Subcommands', value="\n".join(subcommands), inline=False)
|
2016-11-29 16:51:18 +13:00
|
|
|
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send(embed=embed)
|
2016-11-29 16:52:45 +13:00
|
|
|
|
2016-10-08 09:39:41 +13:00
|
|
|
@commands.command()
|
2017-02-12 12:14:57 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-03-06 15:45:44 +13:00
|
|
|
async def motd(self, ctx, *, date=None):
|
2016-10-08 09:39:41 +13:00
|
|
|
"""This command can be used to print the current MOTD (Message of the day)
|
2016-11-29 16:51:18 +13:00
|
|
|
This will most likely not be updated every day, however messages will still be pushed to this every now and then
|
|
|
|
|
|
|
|
EXAMPLE: !motd
|
|
|
|
RESULT: 'This is an example message of the day!'"""
|
2016-10-08 09:39:41 +13:00
|
|
|
if date is None:
|
2017-02-12 12:14:57 +13:00
|
|
|
motd = await utils.get_content('motd')
|
2016-10-08 09:46:11 +13:00
|
|
|
try:
|
2016-10-08 10:29:03 +13:00
|
|
|
# Lets set this to the first one in the list first
|
|
|
|
latest_motd = motd[0]
|
|
|
|
for entry in motd:
|
|
|
|
d = pendulum.parse(entry['date'])
|
|
|
|
|
|
|
|
# Check if the date for this entry is newer than our currently saved latest entry
|
2016-10-08 14:34:05 +13:00
|
|
|
if d > pendulum.parse(latest_motd['date']):
|
2016-10-08 10:29:03 +13:00
|
|
|
latest_motd = entry
|
|
|
|
|
|
|
|
date = latest_motd['date']
|
|
|
|
motd = latest_motd['motd']
|
|
|
|
# This will be hit if we do not have any entries for motd
|
2016-10-08 09:46:11 +13:00
|
|
|
except TypeError:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("No message of the day!")
|
2016-10-08 09:47:10 +13:00
|
|
|
else:
|
|
|
|
fmt = "Last updated: {}\n\n{}".format(date, motd)
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send(fmt)
|
2016-10-08 09:39:41 +13:00
|
|
|
else:
|
|
|
|
try:
|
2017-03-08 17:28:30 +13:00
|
|
|
motd = await utils.get_content('motd', str(pendulum.parse(date).date()))
|
2017-03-08 19:43:40 +13:00
|
|
|
date = motd['date']
|
|
|
|
motd = motd['motd']
|
2016-10-08 09:39:41 +13:00
|
|
|
fmt = "Message of the day for {}:\n\n{}".format(date, motd)
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send(fmt)
|
2016-10-08 09:39:41 +13:00
|
|
|
# This one will be hit if we return None for that day
|
2016-10-08 09:42:46 +13:00
|
|
|
except TypeError:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("No message of the day for {}!".format(date))
|
2016-10-08 09:39:41 +13:00
|
|
|
# This will be hit if pendulum fails to parse the date passed
|
|
|
|
except ValueError:
|
|
|
|
now = pendulum.utcnow().to_date_string()
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("Invalid date format! Try like {}".format(now))
|
2016-10-08 09:39:41 +13:00
|
|
|
|
2016-07-19 11:25:03 +12:00
|
|
|
@commands.command()
|
2017-02-12 12:14:57 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-03-06 15:45:44 +13:00
|
|
|
async def calendar(self, ctx, month: str = None, year: int = None):
|
2016-07-19 11:40:09 +12:00
|
|
|
"""Provides a printout of the current month's calendar
|
2016-11-29 16:51:18 +13:00
|
|
|
Provide month and year to print the calendar of that year and month
|
|
|
|
|
|
|
|
EXAMPLE: !calendar january 2011"""
|
2016-08-27 01:23:02 +12:00
|
|
|
|
|
|
|
# calendar takes in a number for the month, not the words
|
|
|
|
# so we need this dictionary to transform the word to the number
|
2016-07-19 11:25:03 +12:00
|
|
|
months = {
|
|
|
|
"january": 1,
|
|
|
|
"february": 2,
|
|
|
|
"march": 3,
|
|
|
|
"april": 4,
|
|
|
|
"may": 5,
|
|
|
|
"june": 6,
|
|
|
|
"july": 7,
|
|
|
|
"august": 8,
|
|
|
|
"september": 9,
|
|
|
|
"october": 10,
|
|
|
|
"november": 11,
|
|
|
|
"december": 12
|
|
|
|
}
|
2016-08-16 15:30:52 +12:00
|
|
|
# In month was not passed, use the current month
|
2016-07-19 11:25:03 +12:00
|
|
|
if month is None:
|
|
|
|
month = datetime.date.today().month
|
|
|
|
else:
|
2016-07-19 11:30:52 +12:00
|
|
|
month = months.get(month.lower())
|
2016-07-19 11:25:03 +12:00
|
|
|
if month is None:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("Please provide a valid Month!")
|
2016-07-19 11:25:03 +12:00
|
|
|
return
|
2016-08-16 15:30:52 +12:00
|
|
|
# If year was not passed, use the current year
|
2016-07-19 11:25:03 +12:00
|
|
|
if year is None:
|
|
|
|
year = datetime.datetime.today().year
|
2016-08-16 15:30:52 +12:00
|
|
|
# Here we create the actual "text" calendar that we are printing
|
2016-07-19 11:25:03 +12:00
|
|
|
cal = calendar.TextCalendar().formatmonth(year, month)
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("```\n{}```".format(cal))
|
2016-08-27 01:23:02 +12:00
|
|
|
|
2016-08-14 10:46:15 +12:00
|
|
|
@commands.command()
|
2017-02-12 12:14:57 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-03-06 15:45:44 +13:00
|
|
|
async def info(self, ctx):
|
2016-08-14 10:46:15 +12:00
|
|
|
"""This command can be used to print out some of my information"""
|
2016-09-06 14:52:14 +12:00
|
|
|
# fmt is a dictionary so we can set the key to it's output, then print both
|
|
|
|
# The only real use of doing it this way is easier editing if the info
|
|
|
|
# in this command is changed
|
2016-08-27 01:23:02 +12:00
|
|
|
|
2016-11-21 14:27:34 +13:00
|
|
|
# Create the original embed object
|
|
|
|
opts = {'title': 'Dev Server',
|
2016-11-29 21:05:22 +13:00
|
|
|
'description': 'Join the server above for any questions/suggestions about me.',
|
2017-02-12 12:14:57 +13:00
|
|
|
'url': utils.dev_server}
|
2016-11-21 14:27:34 +13:00
|
|
|
embed = discord.Embed(**opts)
|
|
|
|
|
|
|
|
# Add the normal values
|
2017-03-08 12:56:24 +13:00
|
|
|
embed.add_field(name='Total Servers', value=len(self.bot.guilds))
|
2017-03-21 11:27:34 +13:00
|
|
|
embed.add_field(name='Total Members', value=len(self.bot.users))
|
2016-11-21 14:27:34 +13:00
|
|
|
|
|
|
|
# Count the variable values; hangman, tictactoe, etc.
|
2016-12-16 10:43:24 +13:00
|
|
|
hm_games = len(self.bot.get_cog('Hangman').games)
|
2016-11-21 14:27:34 +13:00
|
|
|
|
2016-12-16 10:43:24 +13:00
|
|
|
ttt_games = len(self.bot.get_cog('TicTacToe').boards)
|
|
|
|
|
|
|
|
bj_games = len(self.bot.get_cog('Blackjack').games)
|
2016-11-21 14:28:09 +13:00
|
|
|
|
2016-08-27 01:23:02 +12:00
|
|
|
count_battles = 0
|
|
|
|
for battles in self.bot.get_cog('Interaction').battles.values():
|
|
|
|
count_battles += len(battles)
|
|
|
|
|
|
|
|
if hm_games:
|
2016-11-21 14:28:59 +13:00
|
|
|
embed.add_field(name='Total Hangman games running', value=hm_games)
|
2016-08-27 01:23:02 +12:00
|
|
|
if ttt_games:
|
2016-11-21 14:28:59 +13:00
|
|
|
embed.add_field(name='Total TicTacToe games running', value=ttt_games)
|
2016-08-27 01:23:02 +12:00
|
|
|
if count_battles:
|
2016-11-21 14:28:59 +13:00
|
|
|
embed.add_field(name='Total battles games running', value=count_battles)
|
2016-12-16 10:43:24 +13:00
|
|
|
if bj_games:
|
|
|
|
embed.add_field(name='Total blackjack games running', value=bj_games)
|
2016-11-21 14:27:34 +13:00
|
|
|
|
2017-03-11 11:19:18 +13:00
|
|
|
if hasattr(self.bot, 'uptime'):
|
2017-03-11 09:38:29 +13:00
|
|
|
embed.add_field(name='Uptime', value=(pendulum.utcnow() - self.bot.uptime).in_words())
|
2017-04-21 15:03:52 +12:00
|
|
|
|
|
|
|
memory_usage = self.process.memory_full_info().uss / 1024**2
|
|
|
|
cpu_usage = self.process.cpu_percent() / psutil.cpu_count()
|
|
|
|
embed.add_field(name='Memory Usage', value='{:.2f} MiB'.format(memory_usage))
|
2017-04-21 15:04:48 +12:00
|
|
|
embed.add_field(name='CPU Usage', value='{}%'.format(cpu_usage))
|
2016-11-21 14:27:34 +13:00
|
|
|
embed.set_footer(text=self.bot.description)
|
2016-08-27 01:23:02 +12:00
|
|
|
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send(embed=embed)
|
2016-08-27 01:23:02 +12:00
|
|
|
|
2016-07-31 02:59:35 +12:00
|
|
|
@commands.command()
|
2017-02-12 12:14:57 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-03-06 15:45:44 +13:00
|
|
|
async def uptime(self, ctx):
|
2016-11-29 16:51:18 +13:00
|
|
|
"""Provides a printout of the current bot's uptime
|
|
|
|
|
|
|
|
EXAMPLE: !uptime
|
|
|
|
RESULT: A BAJILLION DAYS"""
|
2017-03-11 11:19:18 +13:00
|
|
|
if hasattr(self.bot, 'uptime'):
|
|
|
|
await ctx.send("Uptime: ```\n{}```".format((pendulum.utcnow() - self.bot.uptime).in_words()))
|
|
|
|
else:
|
|
|
|
await ctx.send("I've just restarted and not quite ready yet...gimme time I'm not a morning pony :c")
|
2016-07-19 11:25:03 +12:00
|
|
|
|
2016-08-08 02:03:36 +12:00
|
|
|
@commands.command(aliases=['invite'])
|
2017-02-12 12:14:57 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-03-06 15:45:44 +13:00
|
|
|
async def addbot(self, ctx):
|
2016-11-29 16:51:18 +13:00
|
|
|
"""Provides a link that you can use to add me to a server
|
|
|
|
|
|
|
|
EXAMPLE: !addbot
|
|
|
|
RESULT: http://discord.gg/yo_mama"""
|
2016-07-10 06:27:14 +12:00
|
|
|
perms = discord.Permissions.none()
|
|
|
|
perms.read_messages = True
|
|
|
|
perms.send_messages = True
|
|
|
|
perms.manage_roles = True
|
|
|
|
perms.ban_members = True
|
|
|
|
perms.kick_members = True
|
|
|
|
perms.manage_messages = True
|
|
|
|
perms.embed_links = True
|
|
|
|
perms.read_message_history = True
|
|
|
|
perms.attach_files = True
|
2017-03-06 15:45:44 +13:00
|
|
|
perms.speak = True
|
|
|
|
perms.connect = True
|
|
|
|
perms.attach_files = True
|
|
|
|
perms.add_reactions = True
|
2016-08-16 15:30:52 +12:00
|
|
|
app_info = await self.bot.application_info()
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("Use this URL to add me to a server that you'd like!\n{}"
|
2017-03-08 11:35:30 +13:00
|
|
|
.format(discord.utils.oauth_url(app_info.id, perms)))
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2017-03-24 16:02:56 +13:00
|
|
|
@commands.command(aliases=['rc'])
|
|
|
|
@utils.custom_perms(send_messages=True)
|
|
|
|
async def cat(self, ctx):
|
|
|
|
"""Use this to print a random cat image.
|
|
|
|
|
|
|
|
EXAMPLE: !cat
|
|
|
|
RESULT: A beautiful picture of a cat o3o"""
|
|
|
|
result = await utils.request('http://random.cat/meow')
|
|
|
|
if result is None:
|
|
|
|
await ctx.send("I couldn't connect! Sorry no cats right now ;w;")
|
|
|
|
return
|
|
|
|
filename = result.get('file', None)
|
|
|
|
if filename is None:
|
|
|
|
await ctx.send("I couldn't connect! Sorry no cats right now ;w;")
|
|
|
|
return
|
|
|
|
|
|
|
|
image = await utils.download_image(filename)
|
|
|
|
filename = re.search('.*\/i\/(.*)', filename).group(1)
|
2017-04-09 15:16:12 +12:00
|
|
|
f = discord.File(image, filename=filename)
|
|
|
|
await ctx.send(file=f)
|
2017-03-24 16:02:56 +13:00
|
|
|
|
|
|
|
|
2017-03-24 16:11:22 +13:00
|
|
|
@commands.command(aliases=['dog', 'rd'])
|
2017-02-12 12:14:57 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-03-06 15:45:44 +13:00
|
|
|
async def doggo(self, ctx):
|
2016-07-17 02:20:17 +12:00
|
|
|
"""Use this to print a random doggo image.
|
2016-11-29 16:51:18 +13:00
|
|
|
|
|
|
|
EXAMPLE: !doggo
|
|
|
|
RESULT: A beautiful picture of a dog o3o"""
|
2017-03-24 16:11:22 +13:00
|
|
|
result = await utils.request('http://random.dog', attr='text')
|
|
|
|
try:
|
|
|
|
soup = bs(result, 'html.parser')
|
2017-03-25 09:37:23 +13:00
|
|
|
filename = soup.img.get('src')
|
|
|
|
except (TypeError, AttributeError):
|
2017-03-24 16:11:22 +13:00
|
|
|
await ctx.send("I couldn't connect! Sorry no dogs right now ;w;")
|
|
|
|
return
|
|
|
|
|
|
|
|
image = await utils.download_image("http://random.dog/{}".format(filename))
|
2017-04-09 15:16:12 +12:00
|
|
|
f = discord.File(image, filename=filename)
|
|
|
|
await ctx.send(file=f)
|
2016-08-27 01:23:02 +12:00
|
|
|
|
2016-08-31 10:33:46 +12:00
|
|
|
@commands.command()
|
2017-02-12 12:14:57 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-03-08 11:35:30 +13:00
|
|
|
async def snek(self, ctx):
|
2016-08-08 06:04:52 +12:00
|
|
|
"""Use this to print a random snek image.
|
2016-11-29 16:51:18 +13:00
|
|
|
|
|
|
|
EXAMPLE: !snek
|
|
|
|
RESULT: A beautiful picture of a snek o3o"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# Find a random image based on how many we currently have
|
2016-10-03 07:08:20 +13:00
|
|
|
f = random.SystemRandom().choice(glob.glob('images/snek*'))
|
2016-08-08 06:04:52 +12:00
|
|
|
with open(f, 'rb') as f:
|
2017-04-09 15:16:12 +12:00
|
|
|
await ctx.send(file=discord.File(f))
|
2016-07-18 08:54:31 +12:00
|
|
|
|
2016-07-10 06:27:14 +12:00
|
|
|
@commands.command()
|
2017-02-12 12:14:57 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-03-06 15:45:44 +13:00
|
|
|
async def joke(self, ctx):
|
2016-11-29 16:51:18 +13:00
|
|
|
"""Prints a random riddle
|
|
|
|
|
|
|
|
EXAMPLE: !joke
|
|
|
|
RESULT: An absolutely terrible joke."""
|
2016-08-16 15:30:52 +12:00
|
|
|
# Use the fortune riddles command because it's funny, I promise
|
2016-08-15 14:10:12 +12:00
|
|
|
fortune_command = "/usr/bin/fortune riddles"
|
2016-10-31 07:45:41 +13:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
fortune = subprocess.check_output(
|
|
|
|
fortune_command.split()).decode("utf-8")
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send(fortune)
|
2016-10-31 07:45:41 +13:00
|
|
|
except discord.HTTPException:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
break
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2017-03-06 15:45:44 +13:00
|
|
|
@commands.command()
|
2017-02-12 12:14:57 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-09-29 12:39:34 +13:00
|
|
|
async def roll(self, ctx, notation: str = "d6"):
|
2016-07-11 01:58:03 +12:00
|
|
|
"""Rolls a die based on the notation given
|
2016-11-29 16:51:18 +13:00
|
|
|
Format should be #d#
|
|
|
|
|
|
|
|
EXAMPLE: !roll d50
|
|
|
|
RESULT: 51 :^)"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# Use regex to get the notation based on what was provided
|
2016-07-11 01:58:03 +12:00
|
|
|
try:
|
2016-09-06 14:52:14 +12:00
|
|
|
# We do not want to try to convert the dice, because we want d# to
|
|
|
|
# be a valid notation
|
2016-07-25 03:34:04 +12:00
|
|
|
dice = re.search("(\d*)d(\d*)", notation).group(1)
|
2016-07-12 06:16:18 +12:00
|
|
|
num = int(re.search("(\d*)d(\d*)", notation).group(2))
|
2016-09-06 14:52:14 +12:00
|
|
|
# Check if something like ed3 was provided, or something else entirely
|
|
|
|
# was provided
|
2016-08-13 07:32:38 +12:00
|
|
|
except (AttributeError, ValueError):
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("Please provide the die notation in #d#!")
|
2016-07-11 01:58:03 +12:00
|
|
|
return
|
2016-08-27 01:23:02 +12:00
|
|
|
|
2016-07-17 02:20:17 +12:00
|
|
|
# Dice will be None if d# was provided, assume this means 1d#
|
2016-07-25 03:34:04 +12:00
|
|
|
dice = dice or 1
|
2016-09-06 14:52:14 +12:00
|
|
|
# Since we did not try to convert to int before, do it now after we
|
|
|
|
# have it set
|
2016-07-25 03:34:04 +12:00
|
|
|
dice = int(dice)
|
2016-07-12 06:16:18 +12:00
|
|
|
if dice > 10:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("I'm not rolling more than 10 dice, I have tiny hands")
|
2016-07-11 01:58:03 +12:00
|
|
|
return
|
2016-07-12 06:16:18 +12:00
|
|
|
if num > 100:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("What die has more than 100 sides? Please, calm down")
|
2016-07-11 01:58:03 +12:00
|
|
|
return
|
2016-10-16 08:43:45 +13:00
|
|
|
if num <= 1:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("A {} sided die? You know that's impossible right?".format(num))
|
2016-10-16 08:43:45 +13:00
|
|
|
return
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
nums = [random.SystemRandom().randint(1, num) for _ in range(0, int(dice))]
|
2017-03-06 15:49:56 +13:00
|
|
|
total = sum(nums)
|
|
|
|
value_str = ", ".join("{}".format(x) for x in nums)
|
2016-07-11 09:18:30 +12:00
|
|
|
|
2016-07-25 03:34:04 +12:00
|
|
|
if dice == 1:
|
2016-07-12 06:13:25 +12:00
|
|
|
fmt = '{0.message.author.name} has rolled a {2} sided die and got the number {3}!'
|
2016-07-11 01:58:03 +12:00
|
|
|
else:
|
2017-03-06 15:49:56 +13:00
|
|
|
fmt = '{0.message.author.name} has rolled {1}, {2} sided dice and got the numbers {3}, for a total of {4}!'
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send(fmt.format(ctx, dice, num, value_str, total))
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2016-07-13 03:58:57 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Core(bot))
|