Skip to content

Commit

Permalink
Merge pull request #64 from ufosc/develop
Browse files Browse the repository at this point in the history
Add clear command update checks
  • Loading branch information
hjarrell authored Jul 1, 2019
2 parents 7dfb70a + 891b751 commit ac343a0
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 20 deletions.
3 changes: 3 additions & 0 deletions cogs/CONSTANTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
REACTION_ERROR = '\u26a0' #
REACTION_NOT_FOUND = '\u2139' # Same as REACTION_INFO
REACTION_INFO = '\u2139' # Same as REACTION_NOT_FOUND

# Roles
OFFICER_ROLE = 436913087245975553
Empty file added cogs/__init__.py
Empty file.
12 changes: 6 additions & 6 deletions cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from contextlib import redirect_stdout
import io

import cogs.util
from cogs.CONSTANTS import OFFICER_ROLE

class Admin(commands.Cog, name='Admin'):

def __init__(self, bot):
self.bot = bot

@commands.command(hidden=True)
@commands.check(cogs.util.is_officer_check)
@commands.has_role(OFFICER_ROLE)
async def load(self, ctx, extension_name : str):
"""Loads an extension."""
try:
Expand All @@ -27,7 +27,7 @@ async def load(self, ctx, extension_name : str):
await ctx.send("{} loaded.".format(extension_name))

@commands.command(hidden=True)
@commands.check(cogs.util.is_officer_check)
@commands.has_role(OFFICER_ROLE)
async def unload(self, ctx, extension_name : str):
"""Unloads an extension."""
if extension_name.startswith("cogs."):
Expand All @@ -37,7 +37,7 @@ async def unload(self, ctx, extension_name : str):
await ctx.send("{} unloaded.".format(extension_name))

@commands.command(hidden=True)
@commands.check(cogs.util.is_officer_check)
@commands.has_role(OFFICER_ROLE)
async def reload(self, ctx, extension_name : str):
"""Unloads and then loads an extension"""
try:
Expand All @@ -51,12 +51,12 @@ async def reload(self, ctx, extension_name : str):
await ctx.send("{} reloaded.".format(extension_name))

@commands.command(hidden=True)
@commands.check(cogs.util.is_officer_check)
@commands.has_role(OFFICER_ROLE)
async def whereami(self, ctx):
await ctx.send("You are in {} with id {}".format(ctx.channel.name, ctx.channel.id))

@commands.command(hidden=True, name="eval")
@commands.check(cogs.util.is_owner)
@commands.is_owner()
async def admin_eval(self, ctx, *, cmd : str):
"""Evaluates Python code only if the executor is hjarrell"""
env = {
Expand Down
4 changes: 2 additions & 2 deletions cogs/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests
import json

import cogs.util
from CONSTANTS import OFFICER_ROLE

class Compile(commands.Cog, name='Compile'):

Expand Down Expand Up @@ -42,7 +42,7 @@ async def _compile(self, ctx, lang_id : int, *, program : str):
await ctx.send("Program failed with output:\n\n```json\n{}\n```".format(json.dumps(response, sort_keys=True, indent=4)))

@commands.command(name="compdebug")
@commands.check(cogs.util.is_officer_check)
@commands.has_role(OFFICER_ROLE)
async def debug_compile(self, ctx):
"""Toggles whether to print the full compile output"""
self.is_debug = not self.is_debug
Expand Down
6 changes: 3 additions & 3 deletions cogs/memes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import praw
from discord.ext import commands

import cogs.util
from cogs.CONSTANTS import OFFICER_ROLE

class Memes(commands.Cog, name='Memes'):
"""All meme related commands"""
Expand Down Expand Up @@ -32,15 +32,15 @@ async def about(self, ctx):
await ctx.send(file=discord.File('alligator.jpg'))

@commands.command()
@commands.check(cogs.util.is_officer_check)
@commands.has_role(OFFICER_ROLE)
async def randplaying(self, ctx):
"""Randomly changes the playing text"""
new_playing = random.choice(self.playing_strings)
await self.bot.change_presence(activity=discord.Game(name=new_playing))
await ctx.send('I am now {}'.format(new_playing))

@commands.command()
@commands.check(cogs.util.is_officer_check)
@commands.has_role(OFFICER_ROLE)
async def setplaying(self, ctx, *, playing: str):
"""Lets an officer set the playing text"""
await self.bot.change_presence(activity=discord.Game(name=playing))
Expand Down
67 changes: 67 additions & 0 deletions cogs/messages.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import discord
from discord.ext import commands

Expand Down Expand Up @@ -72,6 +73,72 @@ async def on_message(self, msg):
except discord.HTTPException as e:
await msg.channel.send('Cannot post answer due to excessive character count! Maximum factorial allowed is `801!`.')


class ALBotMessageClear(commands.Cog, name='Message Clear'):
"""Functions for handling message deletion in channels"""
def __init__(self, bot):
self.bot = bot

@commands.has_role(CONSTANTS.OFFICER_ROLE)
@commands.command()
async def clear(self, ctx, a_number):
# Checks if number is positive int
if not a_number.isdigit() or not int(a_number) > 0:
await ctx.channel.send(content="Please input a number larger than zero")
return

# checks the message reaction to see if the user confirms or cancels the command and returns True or False respectively
async def confirms(self, ctx, user, bot_msg):
while True:

def check(reaction: discord.Reaction, adder: discord.User) -> bool:
return adder == user and reaction.message.id == bot_msg.id

reaction, adder = await self.bot.wait_for('reaction_add', timeout=30.0, check=check)

if reaction.emoji == "✅":
return True
elif reaction.emoji == "❌":
return False

# checks user permissions to see if they can manage messages in the channel
if self.bot.get_channel(ctx.channel.id).permissions_for(ctx.author).manage_messages:
user = ctx.channel.last_message.author
user_msg = ctx.channel.last_message

# warns the user and confirms the clear command
await ctx.channel.send("WARNING: You are about to delete {} messages, are you sure you want to do this?".format(a_number))
bot_msg = ctx.channel.last_message

#adds reactions to the bot message
reactions = ["✅","❌"]
for emoji in reactions:
await bot_msg.add_reaction(emoji)

# Waits 30s for a user reaction and continues only if they respond with ❌ or ✅
try:
cont = await confirms(self, ctx, user, bot_msg)
except asyncio.TimeoutError:
await bot_msg.delete()
await ctx.channel.send('Clear command Timeout')
return

# Cancels the command and deletes the bot message
if not cont:
await bot_msg.delete()
await ctx.channel.send(content='Clear command cancelled')
return

# deletes bot message, user msg, then loops through channel deleting messages
await bot_msg.delete()
await user_msg.delete()
async for message in ctx.channel.history(limit=int(a_number)):
if not message.pinned:
await message.delete()
await asyncio.sleep(0.4)
await ctx.channel.send(content='@{} Successfully deleted {} messages'.format(ctx.author, int(a_number)))

def setup(bot):
bot.add_cog(ALBotMessageDeletionHandlers(bot, SQLConnection()))
bot.add_cog(ALBotFactorialHandler(bot))
bot.add_cog(ALBotMessageClear(bot))
9 changes: 0 additions & 9 deletions cogs/util.py

This file was deleted.

Empty file added database/__init__.py
Empty file.

0 comments on commit ac343a0

Please sign in to comment.