Created
March 31, 2018 16:48
-
-
Save Zwork101/5ef0f272994f701aa74a95b35d309192 to your computer and use it in GitHub Desktop.
my_cogs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from discord.ext import commands | |
from trackerbot.constants import ADMIN_ROLE | |
from trackerbot.utils.decorators import require_roles | |
class Debug: | |
def __init__(self, bot: commands.Bot): | |
self.bot = bot | |
@require_roles(ADMIN_ROLE) | |
@commands.command() | |
async def echo(self, *msgs): | |
await self.bot.say(' '.join(msgs)) | |
def setup(bot: commands.Bot): | |
bot.add_cog(Debug(bot)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from traceback import print_exception | |
from sys import exc_info | |
import re | |
from discord.ext import commands | |
from discord.ext.commands.errors import CheckFailure, CommandNotFound, MissingRequiredArgument | |
from asyncpg.exceptions._base import InterfaceError | |
class ErrorHandler: | |
MATCH_CHECK = re.compile(r"The check functions for command (?P<name>[\d\w]+?) failed.") | |
MATCH_NF = re.compile(r"Command (\"|')(?P<name>[\d\w]+?)(\"|') is not found") | |
MATCH_RARG = re.compile(r"(?P<name>[\d\w]+?) is a required argument that is missing.") | |
def __init__(self, bot: commands.Bot): | |
self.bot = bot | |
self.bot.event(self.on_error) | |
async def on_command_error(self, error: Exception, ctx: commands.Context): | |
""" | |
This event will be triggered if a command goes wrong | |
:param ctx: The context that would have been passed into the function that raised an error. | |
:param error: The exception object that would have been raised | |
:return: None | |
""" | |
if type(error) is CheckFailure: | |
func = self.MATCH_CHECK.match(str(error)).group('name') | |
print("Denied {name} access to {func}".format(name=ctx.message.author, func=func)) | |
await self.bot.send_message(ctx.message.channel, | |
"Sorry, but you don't have access to {func}.".format(func=func)) | |
elif type(error) is CommandNotFound: | |
nf = self.MATCH_NF.match((str(error))).group('name') | |
await self.bot.send_message(ctx.message.channel, "Unknown command: {cmd}.".format(cmd=nf)) | |
elif type(error) is MissingRequiredArgument: | |
rarg = self.MATCH_RARG.match(str(error)).group('name') | |
await self.bot.send_message(ctx.message.channel, "Missing required argument: {rarg}".format(rarg=rarg)) | |
else: | |
print("Uncaught exception:\n{name}: {msg}".format(name=error.__class__.__name__, msg=str(error))) | |
async def on_error(self, error, *args, **kwargs): | |
etype, value, tb = exc_info() | |
if etype is InterfaceError: | |
pass | |
else: | |
print_exception(etype, value, tb) | |
def setup(bot: commands.Bot): | |
bot.add_cog(ErrorHandler(bot)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from discord.ext import commands | |
from discord.member import Member | |
from discord.utils import get | |
from trackerbot.constants import SERVER_ID, GENERAL_CHANNEL | |
class MemberJoin: | |
def __init__(self, bot: commands.Bot): | |
self.bot = bot | |
async def on_member_join(self, member: Member): | |
print(member.name, "joined server:", member.server) | |
if member.server.id == SERVER_ID: | |
channel = get(member.server.channels, id=GENERAL_CHANNEL) | |
await self.bot.send_message(channel, "Welcome <@{id}>, enjoy your stay!".format(id=member.id)) | |
await self.bot.db.member_join(member.server.id, member.id) | |
def setup(bot: commands.Bot): | |
bot.add_cog(MemberJoin(bot)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from discord.ext import commands | |
from discord.member import Member | |
class MemberLeave: | |
def __init__(self, bot: commands.Bot): | |
self.bot = bot | |
async def on_member_remove(self, member: Member): | |
print(member.name, "left server:", member.server.name) | |
await self.bot.db.member_leave(member.server.id, member.id) | |
def setup(bot: commands.Bot): | |
bot.add_cog(MemberLeave(bot)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from discord.ext import commands | |
class MemberQuery: | |
def __init__(self, bot: commands.Bot): | |
self.bot = bot | |
@commands.command(pass_context=True) | |
async def i_joined(self, ctx: commands.Context): | |
now = datetime.now() | |
count = 0 | |
for row in await self.bot.db.member_joins(ctx.message.author.id): | |
if row['date'].day == now.day and row['date'].month == now.month and row['date'].year == now.year: | |
count += 1 | |
await self.bot.say("You joined {count} {plural} with Trackerbot today.".format( | |
count=count, plural="servers" if count != 1 else "server")) | |
@commands.command(pass_context=True) | |
async def i_sent(self, ctx: commands.Context): | |
now = datetime.now() | |
count = 0 | |
for row in await self.bot.db.member_messages(ctx.message.author.id): | |
if row['date'].day == now.day and row['date'].month == now.month and row['date'].year == now.year: | |
count += 1 | |
await self.bot.say("You sent {count} {plural} in servers with Trackerbot today.".format( | |
count=count, plural="messages" if count != 1 else "message" | |
)) | |
@commands.command(pass_context=True) | |
async def i_left(self, ctx: commands.Context): | |
now = datetime.now() | |
count = 0 | |
for row in await self.bot.db.member_leaves(ctx.message.author.id): | |
if row['date'].day == now.day and row['date'].month == now.month and row['date'].year == now.year: | |
count += 1 | |
await self.bot.say("You left {count} {plural} with Trackerbot today.".format( | |
count=count, plural="servers" if count != 1 else "server" | |
)) | |
def setup(bot: commands.Bot): | |
bot.add_cog(MemberQuery(bot)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from discord.ext import commands | |
from discord.message import Message | |
class MessageSent: | |
def __init__(self, bot: commands.Bot): | |
self.bot = bot | |
async def on_message(self, msg: Message): | |
if msg.server is not None: | |
await self.bot.db.new_msg(msg.server.id, msg.channel.id, msg.author.id) | |
def setup(bot: commands.Bot): | |
bot.add_cog(MessageSent(bot)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from discord.ext import commands | |
from trackerbot.utils.decorators import inside_guild | |
class QueryServer: | |
def __init__(self, bot: commands.Bot): | |
self.bot = bot | |
@inside_guild() | |
@commands.command(pass_context=True) | |
async def today_joins(self, ctx: commands.Context): | |
count = 0 | |
for row in await self.bot.db.get_joins(ctx.message.server.id): | |
now = datetime.now() | |
if row['date'].year == now.year and row['date'].month == now.month and row['date'].day == now.day: | |
count += 1 | |
await self.bot.say("{count} {plural} joined today.".format( | |
count=count, plural="members have" if count != 1 else "member has")) | |
@inside_guild() | |
@commands.command(pass_context=True) | |
async def today_messages(self, ctx: commands.Context): | |
count = 0 | |
for row in await self.bot.db.get_messages(ctx.message.server.id): | |
now = datetime.now() | |
if row['date'].year == now.year and row['date'].month == now.month and row['date'].day == now.day: | |
count += 1 | |
await self.bot.say("{count} {plural} been sent today.".format( | |
count=count, plural="messages have" if count != 1 else "message had")) | |
@inside_guild() | |
@commands.command(pass_context=True) | |
async def today_leaves(self, ctx: commands.Context): | |
count = 0 | |
for row in await self.bot.db.get_leaves(ctx.message.server.id): | |
now = datetime.now() | |
if row['date'].year == now.year and row['date'].month == now.month and row['date'].day == now.day: | |
count += 1 | |
await self.bot.say("{count} {plural} left today.".format( | |
count=count, plural="members have" if count != 1 else "member has")) | |
def setup(bot: commands.Bot): | |
bot.add_cog(QueryServer(bot)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment