1
0
Fork 0
mirror of synced 2024-05-20 12:32:26 +12:00
Bonfire/cogs/core.py

195 lines
7.8 KiB
Python
Raw Normal View History

import discord
2016-07-09 13:27:19 +12:00
from discord.ext import commands
2016-07-16 08:13:26 +12:00
from .utils import checks
2016-08-14 10:46:45 +12:00
from .utils import config
2016-07-09 13:27:19 +12:00
import subprocess
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
import pendulum
2016-07-19 11:25:03 +12:00
import datetime
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:
"""Core commands, these are the miscallaneous commands that don't fit into other categories'"""
2016-07-09 13:27:19 +12:00
def __init__(self, bot):
self.bot = bot
2016-07-29 11:08:06 +12:00
2016-07-19 11:25:03 +12:00
@commands.command()
@checks.custom_perms(send_messages=True)
async def calendar(self, month: str = None, year: int = None):
"""Provides a printout of the current month's calendar
2016-07-19 11:25:03 +12:00
Provide month and year to print the calendar of that year and month"""
# 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
}
# 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:
month = months.get(month.lower())
2016-07-19 11:25:03 +12:00
if month is None:
await self.bot.say("Please provide a valid Month!")
return
# 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
# 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)
await self.bot.say("```\n{}```".format(cal))
@commands.command()
@checks.custom_perms(send_messages=True)
async def info(self):
"""This command can be used to print out some of my information"""
# 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
fmt = {}
2016-09-01 16:01:45 +12:00
bot_data = await config.get_content('bot_data')
total_data = {}
for shard, values in bot_data.items():
for key, value in values.items():
if key in total_data:
total_data[key] += value
else:
total_data[key] = value
# We can pretty safely assume that the author is going to be in at least one channel with the bot
# So find the author based on that list
fmt['Official Bot Server'] = config.dev_server
fmt['Uptime'] = (pendulum.utcnow() - self.bot.uptime).in_words()
fmt['Total Servers'] = total_data.get('server_count')
fmt['Total Members'] = total_data.get('member_count')
fmt['Description'] = self.bot.description
servers_playing_music = len([server_id for server_id, state in self.bot.get_cog('Music').voice_states.items() if
state.is_playing()])
hm_games = len([server_id for server_id, game in self.bot.get_cog('Hangman').games.items()])
ttt_games = len([server_id for server_id, game in self.bot.get_cog('TicTacToe').boards.items()])
count_battles = 0
for battles in self.bot.get_cog('Interaction').battles.values():
count_battles += len(battles)
information = "\n".join("{}: {}".format(key, result) for key, result in fmt.items())
information += "\n"
if servers_playing_music:
information += "Playing songs in {} different servers\n".format(servers_playing_music)
if hm_games:
information += "{} different hangman games running\n".format(hm_games)
if ttt_games:
information += "{} different TicTacToe games running\n".format(ttt_games)
if count_battles:
information += "{} different battles going on\n".format(count_battles)
2016-08-14 10:48:49 +12:00
await self.bot.say("```\n{}```".format(information))
2016-07-31 02:59:35 +12:00
@commands.command()
@checks.custom_perms(send_messages=True)
2016-07-31 02:59:35 +12:00
async def uptime(self):
"""Provides a printout of the current bot's uptime"""
await self.bot.say("Uptime: ```\n{}```".format((pendulum.utcnow() - self.bot.uptime).in_words()))
2016-07-19 11:25:03 +12:00
@commands.command(aliases=['invite'])
@checks.custom_perms(send_messages=True)
async def addbot(self):
"""Provides a link that you can use to add me to a server"""
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
app_info = await self.bot.application_info()
await self.bot.say("Use this URL to add me to a server that you'd like!\n{}"
.format(discord.utils.oauth_url(app_info.id, perms)))
2016-08-31 10:33:46 +12:00
@commands.command()
@checks.custom_perms(send_messages=True)
2016-08-31 10:33:46 +12:00
async def doggo(self):
2016-07-17 02:20:17 +12:00
"""Use this to print a random doggo image.
Doggo is love, doggo is life."""
# Find a random image based on how many we currently have
f = glob.glob('images/doggo*')[random.SystemRandom().randint(0, len(glob.glob('images/doggo*')) - 1)]
with open(f, 'rb') as f:
await self.bot.upload(f)
2016-08-31 10:33:46 +12:00
@commands.command()
@checks.custom_perms(send_messages=True)
2016-08-31 10:33:46 +12:00
async def snek(self):
2016-08-08 06:04:52 +12:00
"""Use this to print a random snek image.
Sneks are o3o"""
# Find a random image based on how many we currently have
f = glob.glob('images/snek*')[random.SystemRandom().randint(0, len(glob.glob('images/snek*')) - 1)]
2016-08-08 06:04:52 +12:00
with open(f, 'rb') as f:
await self.bot.upload(f)
@commands.command()
@checks.custom_perms(send_messages=True)
2016-07-09 13:27:19 +12:00
async def joke(self):
"""Prints a random riddle"""
# Use the fortune riddles command because it's funny, I promise
fortune_command = "/usr/bin/fortune riddles"
fortune = subprocess.check_output(fortune_command.split()).decode("utf-8")
await self.bot.say(fortune)
2016-07-09 13:27:19 +12:00
@commands.command(pass_context=True)
@checks.custom_perms(send_messages=True)
async def roll(self, ctx, notation: str = "d6"):
"""Rolls a die based on the notation given
Format should be #d#"""
# Use regex to get the notation based on what was provided
try:
# We do not want to try to convert the dice, because we want d# to be a valid notation
dice = re.search("(\d*)d(\d*)", notation).group(1)
num = int(re.search("(\d*)d(\d*)", notation).group(2))
# Check if something like ed3 was provided, or something else entirely was provided
except (AttributeError, ValueError):
await self.bot.say("Please provide the die notation in #d#!")
return
2016-07-17 02:20:17 +12:00
# Dice will be None if d# was provided, assume this means 1d#
dice = dice or 1
# Since we did not try to convert to int before, do it now after we have it set
dice = int(dice)
if dice > 10:
await self.bot.say("I'm not rolling more than 10 dice, I have tiny hands")
return
if num > 100:
await self.bot.say("What die has more than 100 sides? Please, calm down")
return
value_str = ", ".join(str(random.SystemRandom().randint(1, num)) for i in range(0, int(dice)))
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}!'
else:
2016-07-12 05:58:53 +12:00
fmt = '{0.message.author.name} has rolled {1}, {2} sided dice and got the numbers {3}!'
await self.bot.say(fmt.format(ctx, dice, num, value_str))
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))