Added nise-discord bot, for now it only adds a role when users react in the #changelog channel

This commit is contained in:
nise.moe 2024-02-26 14:14:26 +01:00
parent 6d2b030383
commit 08f2346836
5 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,11 @@
venv/
.idea/
*.pyc
*.log
.git/
.pytest_cache/
cache/
.env
image.png
text_image.png
modified_prefectures.svg

2
nise-discord/Build.sh Executable file
View File

@ -0,0 +1,2 @@
docker build . -t git.gengo.tech/nuff/nise-discord:latest
docker push git.gengo.tech/nuff/nise-discord:latest

22
nise-discord/Dockerfile Normal file
View File

@ -0,0 +1,22 @@
# Use the specified Python base image
FROM python:3.11-slim
ENV version=2
# Set a working directory
WORKDIR /app
RUN apt update
# Copy only the requirements.txt file first to leverage Docker cache
COPY requirements.txt ./requirements.txt
# Upgrade pip and install required python packages
RUN pip3 install --upgrade pip && \
pip3 install -r requirements.txt
# Copy only the src directory, if you're sure all your code resides there
COPY . ./src/
# Set the command to run your application
CMD ["python", "src/bot.py"]

79
nise-discord/bot.py Normal file
View File

@ -0,0 +1,79 @@
import asyncio
import logging
import os
import sys
import discord
from discord.ext import commands
def setup_logging() -> None:
logging.getLogger("discord").setLevel(logging.INFO)
logging.getLogger("discord.http").setLevel(logging.WARNING)
log = logging.getLogger()
log.setLevel(logging.INFO)
log_format = logging.Formatter("%(asctime)s %(levelname)s - %(message)s", '%Y-%m-%d %H:%M:%S')
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(log_format)
log.addHandler(stdout_handler)
log.info('Done setting up logging...')
class NiseBot(commands.Bot):
def __init__(self, bot_loop):
intents = discord.Intents.default()
intents.members = True
super().__init__(
command_prefix='!',
help_command=None,
intents=intents
)
self.loop = bot_loop
self.log = logging.getLogger()
self.reaction_channel_id = 1203671883728293999
self.reaction_emoji_id = 1211660688947417138
async def on_ready(self):
self.log.info('Bot loaded!')
def run(self):
super().run(os.getenv("DISCORD_TOKEN"), reconnect=True)
async def on_raw_reaction_add(self, payload):
if payload.channel_id != self.reaction_channel_id or payload.emoji.id != self.reaction_emoji_id:
return
role = discord.utils.get(payload.member.guild.roles, name="ping-when-update")
await payload.member.add_roles(role)
async def on_raw_reaction_remove(self, payload):
if payload.channel_id != self.reaction_channel_id or payload.emoji.id != self.reaction_emoji_id:
return
guild_id = payload.guild_id
guild = discord.utils.find(lambda g: g.id == guild_id, self.guilds)
user_id = payload.user_id
member = guild.get_member(user_id)
role = discord.utils.get(guild.roles, name="ping-when-update")
await member.remove_roles(role)
async def on_command_error(self, ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send('This command is currently on cooldown. Please try again in a second!')
else:
self.log.error(error)
if __name__ == "__main__":
setup_logging()
loop = asyncio.get_event_loop()
bot = NiseBot(bot_loop=loop)
bot.run()

View File

@ -0,0 +1,2 @@
wheel
py-cord==2.4.1