Initial code

This commit is contained in:
Dan 2024-06-18 23:19:31 -04:00
parent 406e18bffe
commit 69a3dfe8ba
2 changed files with 169 additions and 0 deletions

105
commands.py Normal file
View File

@ -0,0 +1,105 @@
import discord
from discord import app_commands
from discord.ext import commands
import aiosqlite
import os
import sys
import subprocess
class ModCommands(commands.Cog):
def __init__(self, bot):
self.bot = bot
@app_commands.command(name="addnote", description="Add a note to a user")
async def add_note(self, interaction: discord.Interaction,
user: discord.User, note: str):
async with aiosqlite.connect("bot.db") as db:
cursor = await db.execute(
"SELECT notes FROM user_notes WHERE user_id = ?",
(user.id,)
)
row = await cursor.fetchone()
if row:
notes = row[0] + "\n" + note
await db.execute(
"UPDATE user_notes SET notes = ? WHERE user_id = ?",
(notes, user.id)
)
else:
await db.execute(
"INSERT INTO user_notes (user_id, notes, strikes) "
"VALUES (?, ?, ?)",
(user.id, note, 0)
)
await db.commit()
await interaction.response.send_message(
f"Note added for {user.name}: {note}"
)
@app_commands.command(name="warn", description="Warn a user")
async def warn_user(self, interaction: discord.Interaction,
user: discord.User, reason: str):
async with aiosqlite.connect("bot.db") as db:
cursor = await db.execute(
"SELECT strikes FROM user_notes WHERE user_id = ?",
(user.id,)
)
row = await cursor.fetchone()
if row:
strikes = row[0] + 1
await db.execute(
"UPDATE user_notes SET strikes = ? WHERE user_id = ?",
(strikes, user.id)
)
else:
strikes = 1
await db.execute(
"INSERT INTO user_notes (user_id, notes, strikes) "
"VALUES (?, ?, ?)",
(user.id, "", strikes)
)
await db.commit()
await interaction.response.send_message(
f"User {user.name} warned for: {reason}."
f"They now have {strikes} strikes."
)
await user.send(
f"You have been warned for: {reason}."
f"You now have {strikes} strikes."
)
@app_commands.command(name="checknotes",
description="Check notes and strikes of a user")
async def check_notes(self, interaction: discord.Interaction,
user: discord.User):
async with aiosqlite.connect("bot.db") as db:
cursor = await db.execute(
"SELECT notes, strikes FROM user_notes WHERE user_id = ?",
(user.id,)
)
row = await cursor.fetchone()
if row:
notes, strikes = row
await interaction.response.send_message(
f"Notes for {user.name}: {notes}\nStrikes: {strikes}"
)
else:
await interaction.response.send_message(
f"No notes found for {user.name}."
)
@app_commands.command(name="update",
description="Update the bot from the repository")
@commands.is_owner()
async def update(self, interaction: discord.Interaction):
await interaction.response.send_message("Updating the bot...")
# Pull latest changes from the repository
subprocess.run(["git", "pull"])
# Restart the bot
await interaction.followup.send("Restarting the bot...")
os.execv(sys.executable, ['python'] + sys.argv)
async def setup(bot):
await bot.add_cog(ModCommands(bot))

64
main.py Normal file
View File

@ -0,0 +1,64 @@
import discord
from discord.ext import commands
import aiosqlite
import asyncio
import os
import sys
import subprocess
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
class Ariella(discord.Client):
def __init__(self):
super().__init__(intents=intents)
self.tree = discord.app_commands.CommandTree(self)
async def setup_hook(self):
await self.tree.sync()
# Load commands
await self.load_extension('commands')
bot = Ariella()
# Database setup
async def init_db():
async with aiosqlite.connect("bot.db") as db:
await db.execute("""
CREATE TABLE IF NOT EXISTS user_notes (
user_id INTEGER PRIMARY KEY,
notes TEXT,
strikes INTEGER
)
""")
await db.commit()
@bot.event
async def on_ready():
print(f'Logged in as {bot.user} (ID: {bot.user.id})')
print('------')
await init_db()
@bot.event
async def on_guild_join(guild):
await bot.tree.sync(guild=guild)
# Load commands
async def load_commands():
await bot.load_extension('commands')
asyncio.run(load_commands())
bot.run('TOKEN')