Merge branch 'main' of github.com:pvanarkel/discord-jeeves into main

This commit is contained in:
2024-07-16 16:19:00 +02:00
13 changed files with 61 additions and 165 deletions

2
.github/FUNDING.yml vendored
View File

@@ -3,7 +3,7 @@
github: #
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: parkel
ko_fi: pvanarkel
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username

View File

@@ -1,18 +0,0 @@
init:
pip install -r requirements.txt
install:
mkdir logs
cp jeevesbot/env.py.dist jeevesbot/env.py
cp jeevesbot/secret.json.dist jeevesbot/secret.json
clearlog:
rm logs/jeeves.log*
clean:
rm logs/jeeves.log*
rm jeevesbot/env.py
rm jeevesbot/secret.json
rm -rf cogs/__pycache__/
rm -rf jeevesbot/__pycache__/
rm -rf __pycache__/

View File

@@ -1 +1,10 @@
# discord-jeeves
## Installation
- Clone this repository to a directory you want the bot to run from
- `pip3 install -r requirements.txt`
- Copy `jeevesbot/env.py.dist` to `jeevesbot/env.py` and change the variables.
- Copy `jeevesbot/secret.json.dist` to `jeevesbot/secret.json` and add your Google Drive API secret.json
- `cp scripts/jeeves.service /etc/systemd/system/jeeves.service` and change the variables to suit your environment.
- `systemctl daemon-reload`
- `systemctl start jeeves.service`

View File

@@ -9,9 +9,6 @@ from discord import User, errors, TextChannel, Forbidden
log = getLogger(__name__)
embed = discord.Embed()
class Admin(commands.Cog):
""" Admin Commands, Use at own risk. """
def __init__(self, bot):

View File

@@ -1,65 +0,0 @@
import discord
from discord.ext import commands
from logging import getLogger
from jeevesbot import env
# setup logging
log = getLogger(__name__)
e = discord.Embed()
class Flair(commands.Cog):
""" This part of the bot is responsible for giving users roles! """
def __init__(self, bot):
self.bot = bot
self.message_id = 1087124044119281675
self.reactions = {
'1' : env.FLAIRROLES[0],
'2' : env.FLAIRROLES[1],
'3' : env.FLAIRROLES[2],
'4' : env.FLAIRROLES[3],
'5' : env.FLAIRROLES[4],
'6' : env.FLAIRROLES[5],
'👍' : env.FLAIRROLES[6],
'👎' : env.FLAIRROLES[7],
'🔞' : env.FLAIRROLES[8],
}
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
try:
if payload.message_id == self.message_id and str(payload.emoji) in self.reactions:
guild = self.bot.get_guild(payload.guild_id)
member = guild.get_member(payload.user_id)
role_id = self.reactions[str(payload.emoji)]
role = guild.get_role(role_id)
flairrole = guild.get_role(env.FLAIRROLE[0])
await member.add_roles(flairrole)
await member.add_roles(role)
log.info(f'Added role "{role}" to {member}')
except:
log.debug("Reaction not found.")
@commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
try:
if payload.message_id == self.message_id and str(payload.emoji) in self.reactions:
guild = self.bot.get_guild(payload.guild_id)
member = guild.get_member(payload.user_id)
role_id = self.reactions[str(payload.emoji)]
role = guild.get_role(role_id)
await member.remove_roles(role)
log.info(f'Removed role "{role}" from {member}')
except:
log.debug("Could not remove role")
@commands.Cog.listener()
async def on_ready(self):
log.info(f'module active')
async def setup(bot):
await bot.add_cog(Flair(bot))

View File

@@ -2,7 +2,9 @@ import discord
from discord.ext import commands
from jeevesbot import functions
from logging import getLogger
import re
import pytube
from pytube.exceptions import RegexMatchError
# setup logging
log = getLogger(__name__)
@@ -11,11 +13,12 @@ log = getLogger(__name__)
e = discord.Embed()
class Gif(commands.Cog):
""" Ensures that high-risk channels don't display embedded links, but only gifs."""
class Preview(commands.Cog):
""" Ensures that high-risk channels don't display embedded links, but only gifs and youtube previews."""
def __init__(self, bot):
self.bot = bot
self.video_id_regex = re.compile(r'(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/|m\.youtube\.com/(?:watch\?v=|embed/|v/))([^"&?/ ]{11})')
@commands.Cog.listener()
@@ -55,6 +58,32 @@ class Gif(commands.Cog):
await message.channel.send(embed=embed)
logline = (str(message.author) + ' requested a gif: ' + str(gif_url))
log.info(logline)
if 'https://youtu' or 'https://m.youtu' or 'https://www.youtu' in message.content():
roles = functions.checkrole(message.author.roles)
channel = functions.checkchannel(message.channel.id)
if channel is True:
if roles is not True:
url = message.content
youtube = pytube.YouTube(url)
video_title = youtube.title
video_author = youtube.author
video_id = self.extract_video_id(url)
if video_id:
embed = discord.Embed()
embed.set_image(url=f"https://img.youtube.com/vi/{video_id}/hqdefault.jpg")
embed.set_author(name=f"{video_author}")
embed.add_field(name=f"", value=f"[{video_title}]({url})")
await message.channel.send(embed=embed)
log.info(f'User {message.author} requested preview for {url}')
def extract_video_id(self, url):
try:
match = self.video_id_regex.search(url)
if match:
return match.group(1)
except pytube.exceptions.RegexMatchError as e:
pass
@commands.Cog.listener()
@@ -63,4 +92,4 @@ class Gif(commands.Cog):
async def setup(bot):
await bot.add_cog(Gif(bot))
await bot.add_cog(Preview(bot))

View File

@@ -1,63 +0,0 @@
import discord
from discord.ext import commands
from logging import getLogger
from jeevesbot import env
# setup logging
log = getLogger(__name__)
embed = discord.Embed()
class BotSetup(commands.Cog):
""" Admin Commands, Use at own risk. """
def __init__(self, bot):
self.bot = bot
self.botchannel = int(env.BOTCHANNEL[0])
@commands.command(hidden=True)
@commands.is_owner()
async def flairsetup(self, ctx):
""" Setup reaction post as embed. """
await ctx.message.delete()
channel_id = ctx.channel.id
if (channel_id) == (self.botchannel):
embed = discord.Embed(title="Kies je rollen!",
description="Klik op de emoji onder het bericht om de rol te krijgen.")
embed.add_field(name="", value="", inline=False)
embed.add_field(name="Voornaamwoorden:", value="", inline=False)
embed.add_field(name=":one:", value="hij/hem")
embed.add_field(name=":two:", value="zij/haar")
embed.add_field(name=":three:", value="hen/hun")
embed.add_field(name=":four:", value="die/hun")
embed.add_field(name=":five:", value="die/diens")
embed.add_field(name=":six:", value="iedere/all")
embed.add_field(name="Sta je open voor Direct Messages van andere serverleden?", value="", inline=False)
embed.add_field(name=":thumbsup:", value="DM: ja")
embed.add_field(name=":thumbsdown:", value="DM: nee")
# embed.add_field(name="Rollen voor toegang tot opt-in kanalen.", value="", inline=False)
# embed.add_field(name=":underage:", value="serieuze-onderwerpen")
embed.set_footer(text="Mis je een voornaamwoord of heb je ideeën voor een andere rol? Laat het de mods weten in de #ideeënbus!\nJe kan zelf je rollen verwijderen door opnieuw op de emoji te drukken.")
message = await ctx.send(embed=embed)
await message.add_reaction("1")
await message.add_reaction("2")
await message.add_reaction("3")
await message.add_reaction("4")
await message.add_reaction("5")
await message.add_reaction("6")
await message.add_reaction("👍")
await message.add_reaction("👎")
# await message.add_reaction("🔞")
log.warn(f'{ctx.message.author} reset the flair embed, new message_id = {message.id}')
else:
log.warn(f'{ctx.message.author} tried setting up the flair in the wrong channel')
@commands.Cog.listener()
async def on_ready(self):
log.info(f'module active')
async def setup(bot):
await bot.add_cog(BotSetup(bot))

View File

@@ -14,9 +14,4 @@ All purge commands also delete their own message.
---
`!flairsetup`
Can be used to reset the flair embed for Jeeves. Needs manual steps to fix the bot giving out roles. Don't use unless you know what you're doing.
---
*To easily copy IDs of discord items, turn on Developer Mode in Advanced settings in your profile. After this, you can right click on almost all items in discord to get the "Copy ID" option.*

View File

@@ -5,7 +5,6 @@ Dit zorgt er helaas wel voor dat GIFjes in die kanalen niet werken. Door Jeeves
** Flair **
In het #rollen kanaal staat een bericht met daar onder emoji. Als je een voornaamwoord of andere flair rol in je account wil hebben, kun je op de emoji klikken en dan krijg je de rol automatisch.
Wil je de rol weg hebben? Klik dan nogmaals op de emoji en de rol wordt weg gehaald!
** Extra informatie voor het !roll commando **

View File

@@ -2,7 +2,7 @@
TOKEN = 'discord-bot-token-here'
ADMIN_ROLE = 'role-to-exclude-from-gifbot'
GIFCHANNELS = 'add-channel-ids-for-bot-to-work-in'
BOTCHANNEL = ['add id of bot channel']
FLAIRROLES = ['add list of role ids to correspond to the emoji']
FLAIRROLE = ['add id of flair separator role']
PREVIEWCHANNELS = [add-channel-ids-for-bot-to-work-in]
BOTCHANNEL = [add id of bot channel]
FLAIRROLES = [add list of role ids to correspond to the emoji]
FLAIRROLE = [add id of flair separator role]

View File

@@ -25,7 +25,7 @@ def checkrole(roles):
# check if the source channel is in the list of channels that are watched by the bot.
def checkchannel(channelid):
if channelid in env.GIFCHANNELS:
if channelid in env.PREVIEWCHANNELS:
return True
else:
return False

View File

@@ -11,6 +11,7 @@ pylint
gspread
oauth2client
Pillow
pytube
# needs this version, otherwise TypeErrors will break stuff
yarl==1.4.2

12
scripts/jeeves.service Normal file
View File

@@ -0,0 +1,12 @@
[Unit]
Description=Jeeves Discord Bot
After=network.target
[Service]
User=<username>
WorkingDirectory=/path/to/bot/directory
ExecStart=/usr/bin/python3 /path/to/bot/directory/bot.py
Restart=always
[Install]
WantedBy=multi-user.target