refactor code to use cogs instead of loose functions
This commit is contained in:
53
cogs/games.py
Normal file
53
cogs/games.py
Normal 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
69
cogs/gif.py
Normal 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
39
cogs/links.py
Normal 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))
|
||||||
|
|
||||||
110
jeeves.py
110
jeeves.py
@@ -3,7 +3,8 @@
|
|||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
import logging
|
import logging
|
||||||
from jeevesbot import bothelp, functions, env, babbelbingo
|
from jeevesbot import env
|
||||||
|
import os
|
||||||
|
|
||||||
# setup logging
|
# setup logging
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
@@ -15,90 +16,43 @@ logger.addHandler(handler)
|
|||||||
|
|
||||||
# setup discord.py bot
|
# setup discord.py bot
|
||||||
intents = discord.Intents().all()
|
intents = discord.Intents().all()
|
||||||
bot = commands.Bot(command_prefix='!', intents=intents, help_command=None)
|
bot = commands.Bot(command_prefix='!', intents=intents)
|
||||||
e = discord.Embed()
|
e = discord.Embed()
|
||||||
|
|
||||||
@bot.event
|
|
||||||
async def on_message(message):
|
@bot.command()
|
||||||
# we do not want the bot to reply to itself
|
@commands.has_permissions(administrator=True)
|
||||||
if message.author == bot.user:
|
async def load(ctx, extension):
|
||||||
return
|
bot.load_extension(f'cogs.{extension}')
|
||||||
if message.content.startswith('!help'):
|
print(extension, 'module loaded')
|
||||||
parameters = message.content.split(' ', 1)
|
|
||||||
if len(parameters) == 2:
|
|
||||||
msg = bothelp.help(parameters[1])
|
@bot.command()
|
||||||
else:
|
@commands.has_permissions(administrator=True)
|
||||||
msg = bothelp.help()
|
async def unload(ctx, extension):
|
||||||
await message.author.send(msg)
|
bot.unload_extension(f'cogs.{extension}')
|
||||||
logline = (str(message.author) + ' requested help')
|
print(extension, 'module unloaded')
|
||||||
logger.info(logline)
|
|
||||||
# giphy and tenor both have different structures to their links
|
|
||||||
if message.content.startswith('https://tenor.com/'):
|
@bot.command()
|
||||||
roles = functions.checkrole(message.author.roles)
|
@commands.has_permissions(administrator=True)
|
||||||
channel = functions.checkchannel(message.channel.id)
|
async def reload(ctx, extension):
|
||||||
embed_url = message.content
|
bot.unload_extension(f'cogs.{extension}')
|
||||||
follow_url = embed_url + '.gif'
|
bot.load_extension(f'cogs.{extension}')
|
||||||
full_url = await functions.resolve(follow_url)
|
print(extension, 'module reloaded')
|
||||||
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)
|
|
||||||
if message.content.startswith('!roll'):
|
|
||||||
parameters = message.content.split(' ', 1)
|
|
||||||
if len(parameters) == 2:
|
|
||||||
param = parameters[1]
|
|
||||||
roll,result = functions.roll(param)
|
|
||||||
msg = 'Rolling %s for {0.author.mention}: `%s`'.format(message) % (param,roll)
|
|
||||||
await message.channel.send(msg)
|
|
||||||
if message.content.startswith('!bingo'):
|
|
||||||
name = message.author.name
|
|
||||||
bingocard = babbelbingo.bingo(name)
|
|
||||||
guild_id = message.guild.id
|
|
||||||
guild = discord.utils.find(lambda g: g.id == guild_id, bot.guilds)
|
|
||||||
member = discord.utils.get(guild.members, id=message.author.id)
|
|
||||||
role = discord.utils.get(guild.roles , name='babbelbingo')
|
|
||||||
await message.author.send(file=discord.File(bingocard))
|
|
||||||
await member.add_roles(role)
|
|
||||||
if message.content.startswith('!youtube'):
|
|
||||||
msg = 'https://www.youtube.com/channel/UCTAnkjUOud_HdKhY_hi2ISA'
|
|
||||||
await message.channel.send(msg)
|
|
||||||
if message.content.startswith('!blokkenschema'):
|
|
||||||
msg = 'https://www.larp-platform.nl/zomer-festival/blokkenschema/'
|
|
||||||
await message.channel.send(msg)
|
|
||||||
if message.content.startswith('!donatie'):
|
|
||||||
msg = 'https://bunq.me/larpzomerfestival'
|
|
||||||
await message.channel.send(msg)
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print('### Active with id %s as %s ###' % (bot.user.id,bot.user.name) )
|
print('### Active with id %s as %s ###' % (bot.user.id,bot.user.name) )
|
||||||
activity = discord.Activity(name='!help', type=discord.ActivityType.listening)
|
activity = discord.Activity(name='!help', type=discord.ActivityType.listening)
|
||||||
await bot.change_presence(activity=activity)
|
await bot.change_presence(activity=activity)
|
||||||
|
|
||||||
|
|
||||||
|
for filename in os.listdir('./cogs'):
|
||||||
|
if filename.endswith('.py'):
|
||||||
|
bot.load_extension(f'cogs.{filename[:-3]}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
bot.run(env.TOKEN)
|
bot.run(env.TOKEN)
|
||||||
|
|||||||
@@ -74,11 +74,11 @@ def make_bingocard(name, questions):
|
|||||||
draw.multiline_text((497, 756.5), box23, (0, 0, 0), font=font_name, align='center', anchor='mm', spacing=8)
|
draw.multiline_text((497, 756.5), box23, (0, 0, 0), font=font_name, align='center', anchor='mm', spacing=8)
|
||||||
draw.multiline_text((631, 756.5), box24, (0, 0, 0), font=font_name, align='center', anchor='mm', spacing=8)
|
draw.multiline_text((631, 756.5), box24, (0, 0, 0), font=font_name, align='center', anchor='mm', spacing=8)
|
||||||
table = str.maketrans({'(':None, ')':None, '/':None, ' ':'_'})
|
table = str.maketrans({'(':None, ')':None, '/':None, ' ':'_'})
|
||||||
image.save('jeevesbot/files/generated_bingocards/' + name.translate(table) + '.png')
|
image.save('jeevesbot/files/generated_bingocards/' + str(name).translate(table) + '.png')
|
||||||
|
|
||||||
def bingo(name):
|
def bingo(name):
|
||||||
questions = babbelbingo_file()
|
questions = babbelbingo_file()
|
||||||
make_bingocard(name, questions)
|
make_bingocard(name, questions)
|
||||||
table = str.maketrans({'(':None, ')':None, '/':None, ' ':'_'})
|
table = str.maketrans({'(':None, ')':None, '/':None, ' ':'_'})
|
||||||
bingoimage = ('jeevesbot/files/generated_bingocards/' + name.translate(table) + '.png')
|
bingoimage = ('jeevesbot/files/generated_bingocards/' + str(name).translate(table) + '.png')
|
||||||
return bingoimage
|
return bingoimage
|
||||||
|
|||||||
Reference in New Issue
Block a user