121 lines
3.5 KiB
Python
121 lines
3.5 KiB
Python
import sys
|
|
import threading
|
|
import logging
|
|
import discord
|
|
import sqlite3
|
|
|
|
from discord.ext import commands
|
|
from dislash import InteractionClient, Option, OptionType
|
|
|
|
|
|
def insert_varible_into_table(name, userid, isbot, nick, guild):
|
|
try:
|
|
sqlite_connection = sqlite3.connect('user.db')
|
|
cursor = sqlite_connection.cursor()
|
|
cursor.execute(f'''CREATE TABLE IF NOT EXISTS "{str(guild)}"
|
|
([userid] INTEGER PRIMARY KEY, [username] TEXT, [nick] TEXT, [isbot] BOOL, [track] TEXT)
|
|
''')
|
|
|
|
sqlite_insert_with_param = (f"""INSERT OR IGNORE INTO "{str(guild)}"
|
|
(username, userid, nick, isbot)
|
|
VALUES (?, ?, ?, ?)""")
|
|
|
|
data_tuple = (name, userid, nick, isbot)
|
|
cursor.execute(sqlite_insert_with_param, data_tuple)
|
|
sqlite_connection.commit()
|
|
|
|
cursor.close()
|
|
|
|
except sqlite3.Error as error:
|
|
logging.error("Failed to insert Python variable into sqlite table", error)
|
|
|
|
|
|
def _add_audio(guildid, user, audio):
|
|
try:
|
|
sql_conn = sqlite3.connect('user.db')
|
|
cursor = sql_conn.cursor()
|
|
sql_update_query = f"""UPDATE "{str(guildid)}" set track = ? where userid = ?"""
|
|
data_tuple = (audio, user.removesuffix('>').removeprefix('<@'))
|
|
cursor.execute(sql_update_query, data_tuple)
|
|
sql_conn.commit()
|
|
|
|
cursor.close()
|
|
|
|
except sqlite3.Error as error:
|
|
logging.error("Failed to insert Python variable into sqlite table", error)
|
|
|
|
|
|
def _read_db(guildid, user):
|
|
try:
|
|
sql_con = sqlite3.connect("user.db")
|
|
cursor = sql_con.cursor()
|
|
print(user)
|
|
sql_read = f"""SELECT * FROM "{guildid}" where userid = {user}"""
|
|
cursor.execute(sql_read)
|
|
record = cursor.fetchone()
|
|
if record[4] is None:
|
|
return "None"
|
|
else:
|
|
return record[4]
|
|
|
|
except sqlite3.Error as error:
|
|
logging.error("Failed to read sqlite table", error)
|
|
finally:
|
|
if sql_con:
|
|
sql_con.close()
|
|
|
|
|
|
threading.current_thread().name = "main"
|
|
logging.basicConfig(stream=sys.stdout, filemode='w', level='INFO',
|
|
format='%(asctime)s - %(levelname)s - %(threadName)s - %(message)s')
|
|
|
|
intents = discord.Intents.default()
|
|
intents.members = True
|
|
bot = commands.Bot(command_prefix='$', guild_subscriptions=True, intents=intents)
|
|
inter_client = InteractionClient(bot)
|
|
|
|
|
|
@bot.event
|
|
async def on_ready():
|
|
for g in bot.get_all_members():
|
|
insert_varible_into_table(g.name, g.id, g.bot, g.nick, g.guild.id)
|
|
logging.info(f'Bot started')
|
|
logging.info('We have logged in as {0.user}'.format(bot))
|
|
|
|
|
|
@bot.event
|
|
async def on_guild_join(guild):
|
|
for g in guild.members:
|
|
insert_varible_into_table(g.name, g.id, g.bot, g.nick, guild.id)
|
|
|
|
|
|
@bot.event
|
|
async def on_member_join(member):
|
|
insert_varible_into_table(member.name, member.id, member.bot, member.nick, member.guild.id)
|
|
|
|
|
|
@bot.command()
|
|
async def add_audio(ctx, user, audio):
|
|
_add_audio(ctx.guild.id, user, audio)
|
|
|
|
|
|
@bot.command()
|
|
async def test(ctx, user):
|
|
await ctx.send(_read_db(ctx.guild.id, user))
|
|
|
|
|
|
@inter_client.slash_command(
|
|
name="read-audio",
|
|
description="Shows user's profile",
|
|
options=[
|
|
Option("user", "Specify any user", OptionType.USER),
|
|
]
|
|
)
|
|
async def hello(ctx, user):
|
|
await ctx.send(_read_db(ctx.guild.id, user.id))
|
|
|
|
# await ctx.send(_read_db(ctx.guild.id, user))
|
|
|
|
|
|
bot.run('OTQ3OTUzOTAxNzgzNjIxNjYy.GTXbMv.KrztaTO7-ivsPEAVjsyikSQ-GP-ANwULmDraig')
|