This commit is contained in:
2023-11-26 17:40:00 +01:00
parent ca7d2d94a2
commit a44b7cfb2d
2 changed files with 150 additions and 20 deletions

View File

@@ -1,6 +1,6 @@
create table recruits
(
discord_id int(20) unsigned not null,
discord_id bigint not null,
nickname varchar(50) null,
recruit tinyint(1) not null,
id int auto_increment,
@@ -14,8 +14,8 @@ create table no_votes
(
id int auto_increment
primary key,
discord_id_recruit int(20) unsigned not null,
discord_id_voter int(20) unsigned not null,
discord_id_recruit bigint not null,
discord_id_voter bigint not null,
reason longtext not null,
constraint no_votes_pk2
unique (id),
@@ -23,28 +23,22 @@ create table no_votes
foreign key (discord_id_recruit) references recruits (discord_id)
);
create table yes_votes
create table settings
(
id int auto_increment
primary key,
discord_id_recruit int(20) unsigned not null,
discord_id_voter int(20) unsigned not null,
constraint yes_votes_pk2
unique (id),
constraint yes_votes_recruits_discord_id_fk
foreign key (discord_id_recruit) references recruits (discord_id)
on delete cascade
name varchar(40) not null,
value int(20) unsigned not null
);
create table yes_votes
(
id int auto_increment
primary key,
discord_id_recruit int(20) unsigned not null,
discord_id_voter int(20) unsigned not null,
discord_id_recruit bigint not null,
discord_id_voter bigint not null,
constraint yes_votes_pk2
unique (id),
constraint yes_votes_recruits_discord_id_fk
foreign key (discord_id_recruit) references recruits (discord_id)
on delete cascade
);
INSERT INTO settings (name, value) VALUES ("channel_voting", "");

136
main.py Normal file
View File

@@ -0,0 +1,136 @@
import discord
import os # default module
from dotenv import load_dotenv
import json
import mariadb
import sys
from discord import default_permissions
load_dotenv() # load all the variables from the env file
with open('config.json') as user_file:
config = json.loads(user_file.read())
def connect_db():
try:
conn = mariadb.connect(
user=config["db_user"],
password=config["db_password"],
host=config["db_host"],
port=3306,
database=config["db_name"]
)
return conn
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
def recruit_update(member, recruit, cur, conn):
if member.nick is None:
nick = member.name
else:
nick = member.nick
cur.execute("INSERT INTO recruits (discord_id, nickname, recruit) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE nickname=?, recruit=?;", (member.id, nick, recruit, nick, recruit))
conn.commit()
intents = discord.Intents.default()
intents.members = True
bot = discord.Bot(intents = intents)
@bot.event
async def on_ready():
print(f"{bot.user} is ready and online!")
conn = connect_db()
cur = conn.cursor()
# create Slash Command group with bot.create_group
settings = bot.create_group("set", "Einstellungen")
@settings.command()
@discord.commands.default_permissions(manage_roles=True)
async def channel_voting(ctx):
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'channel_voting'", (ctx.channel_id, ))
conn.commit()
await ctx.respond(f"Done.", ephemeral=True)
@settings.command()
@default_permissions(manage_roles=True)
async def channel_report(ctx):
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'channel_report'", (ctx.channel_id, ))
conn.commit()
await ctx.respond(f"Done.", ephemeral=True)
@settings.command()
@default_permissions(manage_roles=True)
async def role_recruit(
ctx,
rolearg: discord.Option(discord.SlashCommandOptionType.role)
):
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'role_recruit'", (rolearg.id, ))
conn.commit()
await ctx.respond(f"Done.", ephemeral=True)
@settings.command()
@default_permissions(manage_roles=True)
async def guild(ctx):
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'guild'", (ctx.guild_id, ))
conn.commit()
await ctx.respond(f"Done.", ephemeral=True)
def check_roles(member, recruit_id):
for role in member.roles:
if role.id == recruit_id:
return "Yes"
@settings.command()
@default_permissions(manage_roles=True)
async def refresh(ctx):
cur.execute("SELECT value FROM settings WHERE name='guild'")
#conn.commit()
guild_id = cur.fetchone()
guild_id = guild_id[0]
cur.execute("SELECT value FROM settings WHERE name='role_recruit'")
recruit_id = cur.fetchone()
recruit_id = recruit_id[0]
guild = bot.get_guild(guild_id)
for role in guild.roles:
if role.id == recruit_id:
for member in role.members:
recruit_update(member, 1, cur, conn)
for member in guild.members:
result = check_roles(member, recruit_id)
if result == "Yes":
None
else:
if member.nick is None:
nick = member.name
else:
nick = member.nick
cur.execute("UPDATE recruits SET recruit = ? WHERE discord_id = ?", (0, member.id))
conn.commit()
await ctx.respond(f"Done.", ephemeral=True)
@bot.event
async def on_member_update(previous, after):
cur.execute("SELECT value FROM settings WHERE name='role_recruit'")
recruit_id = cur.fetchone()
recruit_id = recruit_id[0]
for role in after.roles:
if role.id == recruit_id:
recruit_update(after, 1, cur, conn)
return None
# Kein Rekrut
for role in previous.roles:
if role.id == recruit_id:
recruit_update(after, 0, cur, conn)
return None
bot.run(os.getenv('TOKEN')) # run the bot with the token