refactor code to use cogs instead of loose functions

This commit is contained in:
2021-07-15 22:17:05 +02:00
parent 0395cd908d
commit 0e889261a0
5 changed files with 195 additions and 80 deletions

53
cogs/games.py Normal file
View File

@@ -0,0 +1,53 @@
import discord
from discord.ext import commands
import logging
from jeevesbot import functions, babbelbingo
e = discord.Embed()
# setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('jeeves')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='a')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
class Games(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def roll(self, ctx):
parameters = ctx.message.content.split(' ', 1)
if len(parameters) == 2:
param = parameters[1]
roll,result = functions.roll(param)
msg = f'Rolling %s for {ctx.message.author.mention}: `%s`'.format(roll) % (param,roll)
logline = f'Rolling %s for {ctx.message.author}: `%s`'.format(roll) % (param,roll)
logger.info(logline)
await ctx.message.channel.send(msg)
@commands.command()
@commands.guild_only()
async def bingo(self, ctx):
name = ctx.message.author.name
bingocard = babbelbingo.bingo(name)
guild = ctx.message.guild
member = discord.utils.get(guild.members, id=ctx.message.author.id)
role = discord.utils.get(guild.roles , name='babbelbingo')
await ctx.author.send(file=discord.File(bingocard))
await member.add_roles(role)
@commands.Cog.listener()
async def on_ready(self):
print('##### GAMES module active')
def setup(bot):
bot.add_cog(Games(bot))

69
cogs/gif.py Normal file
View File

@@ -0,0 +1,69 @@
import discord
from discord.ext import commands
import logging
from jeevesbot import functions
e = discord.Embed()
# setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('jeeves')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='a')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
class Gif(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_message(self, message):
if message.content.startswith('https://tenor.com/'):
roles = functions.checkrole(message.author.roles)
channel = functions.checkchannel(message.channel.id)
embed_url = message.content
follow_url = embed_url + '.gif'
full_url = await functions.resolve(follow_url)
gif_url = full_url.split('?')[0]
embed = e.set_image(url=gif_url)
if channel is True:
if roles is not True:
await message.channel.send(embed=embed)
logline = (str(message.author) + ' requested a gif: ' + str(gif_url))
logger.info(logline)
if message.content.endswith('.gif'):
roles = functions.checkrole(message.author.roles)
channel = functions.checkchannel(message.channel.id)
embed_url = message.content
embed = e.set_image(url=embed_url)
if channel is True:
if roles is not True:
await message.channel.send(embed=embed)
logline = (str(message.author) + ' requested a gif: ' + str(embed_url))
logger.info(logline)
if message.content.startswith('https://giphy.com/'):
roles = functions.checkrole(message.author.roles)
channel = functions.checkchannel(message.channel.id)
embed_url = message.content
image_code = embed_url.split('-')[-1]
gif_url = 'https://media.giphy.com/media/' + image_code + '/giphy.gif'
embed = e.set_image(url=gif_url)
if channel is True:
if roles is not True:
await message.channel.send(embed=embed)
logline = (str(message.author) + ' requested a gif: ' + str(gif_url))
logger.info(logline)
@commands.Cog.listener()
async def on_ready(self):
print('##### GIF module active')
def setup(bot):
bot.add_cog(Gif(bot))

39
cogs/links.py Normal file
View File

@@ -0,0 +1,39 @@
import discord
from discord.ext import commands
import logging
e = discord.Embed()
# setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('jeeves')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='a')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
class Links(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def youtube(self, ctx):
await ctx.send('https://www.youtube.com/channel/UCTAnkjUOud_HdKhY_hi2ISA')
@commands.command()
async def donatie(self, ctx):
await ctx.send('<https://bunq.me/larpzomerfestival>')
@commands.Cog.listener()
async def on_ready(self):
print('##### LINKS module active')
def setup(bot):
bot.add_cog(Links(bot))