I'm writing a discord bot and having some issues with it. The idea is that someone clicks a button and a stopwatch starts and keeps track of the time. If you click on the 2nd button, the stopwatch will stop. Then you have a certain time between starting and stopping. If you then click start again, time will be added and so on. I want to have those times in a database. I made this but got some errors. So I need help. Code:
import discord
import aiosqlite
from discord.ext import commands, tasks
import os
intent = discord.Intents.default()
intent.members = True
intent.message_content = True
client = commands.Bot(command_prefix = '!', intents = intent)
time_IamKiaran = 0
time_GwnDaan_ = 0
time_hamburger7439 = 0
@client.event
async def on_ready():
setattr(client,"db", await aiosqlite.connect("Aanwezigheid.db"))
async with client.db.cursor() as cursor:
await cursor.execute("CREATE TABLE IF NOT EXISTS Aanwezigheid (User INTEGER, NuAanwezig BOOLEAN, AanwezigTime INTEGER)")
print("Kloksys. De Klok database verbinding gemaakt!")
await client.db.commit()
await client.db.close()
print("Bot is currently online!")
klokken.klok.start()
class klokken(discord.ui.View):
def init(self):
super().init()
# Dit is de timer loop die elke minuut dat iemand aan wezig is de waarde met 1 verhoogd.
@tasks.loop(seconds=60)
async def klok():
async with aiosqlite.connect("Aanwezigheid.db") as db:
await db.execute("Select AanwezigTime FROM Aanwezigheid",())
aanwezigtime = await db.execute_fetchall()
await db.execute("Select NuAanwezig FROM Aanwezigheid",())
users = await db.execute_fetchall()
await db.execute("UPDATE Aanwezigheid SET AanwezigTime=? WHERE user=? AND NuAanwezig=?",(aanwezigtime[0]+1,users[0],True,))
await db.execute("UPDATE Aanwezigheid SET AanwezigTime=? WHERE user=? AND NuAanwezig=?",(aanwezigtime[1]+1,users[1],True,))
await db.execute("UPDATE Aanwezigheid SET AanwezigTime=? WHERE user=? AND NuAanwezig=?",(aanwezigtime[2]+1,users[2],True,))
await db.commit()
await client.db.close()
# Deze functie wijzigt het bericht met wie er momenteel aanwezig is.
async def messageupdate(self, button: discord.ui.Button, interaction: discord.Interaction):
async with aiosqlite.connect("Aanwezigheid.db") as db:
await db.execute("Select NuAanwezig FROM Aanwezigheid",())
aanwezig = await db.execute_fetchall()
await client.db.close()
await interaction.message.delete()
channel = client.get_channel(945349385997398037)
embed=discord.Embed(title="Aanwezig in winkel", description="Klik op de emoji zodra je in de winkel staat en klik er nog een keer op als je weg gaat uit winkel", color=0x0ad6ff)
embed.set_author(name="Daily Firework")
embed.add_field(name="IamKiaran", value=aanwezig[0], inline=False)
embed.add_field(name="GwnDaan_", value=aanwezig[1], inline=False)
embed.add_field(name="hamburger7439", value=aanwezig[2], inline=False)
await channel.send(embed=embed, view = klokken)
# Deze functie zet een gebruiker op aanwezig.
async def aanwezig(self, button: discord.ui.Button, interaction: discord.Interaction):
async with aiosqlite.connect("Aanwezigheid.db") as db:
await db.execute("UPDATE Aanwezigheid SET NuAanwezig=? WHERE user=?",(True,interaction.user.id,))
await db.commit()
await client.db.close()
await self.messageupdate(button, interaction)
# Deze functie zet een gebruiker op afwezig.
async def afwezig(self, button: discord.ui.Button, interaction: discord.Interaction):
async with aiosqlite.connect("Aanwezigheid.db") as db:
await db.execute("UPDATE Aanwezigheid SET NuAanwezig=? WHERE user=?",(False,interaction.user.id,))
await db.commit()
await client.db.close()
await self.messageupdate(button, interaction)
# Dit zijn de buttons die onder het bericht komen.
@discord.ui.button(
label="Aanwezig",
emoji= "✅",
style=discord.ButtonStyle.green,
custom_id=1,
)
async def aanwezig_button(self, button, interaction):
await self.aanwezig(button, interaction)
@discord.ui.button(
label="afwezig",
emoji= "✖️",
style=discord.ButtonStyle.red,
custom_id=0,
)
async def afwezig_buttom(self, button, interaction):
await self.afwezig(button, interaction)
# Dit is de command waar al het bovenstaande samenkomt
@client.command()
async def aanwezig(ctx):
channel = client.get_channel(945349385997398037)
embed=discord.Embed(title="Aanwezig in winkel", description="Klik op de emoji zodra je in de winkel staat en klik er nog een keer op als je weg gaat uit winkel", color=0x0ad6ff)
embed.set_author(name="Daily Firework")
embed.add_field(name="IamKiaran", value=0, inline=False)
embed.add_field(name="GwnDaan_", value=0, inline=False)
embed.add_field(name="hamburger7439", value=0, inline=False)
await channel.send(embed=embed, view = klokken)
@client.command()
@commands.is_owner()
async def Tijdreset(ctx):
async with aiosqlite.connect("Aanwezigheid.db") as db:
await db.execute("Select AanwezigTijd FROM Aanwezigheid",())
tijden = await db.execute_fetchall()
await db.execute("UPDATE Aanwezigheid SET AanwezigTijd=0 ",())
await db.commit()
await client.db.close()
embed=discord.Embed(title="Aanwezigheid in winkel", description="De tijden die de medewerkers in de afgelopen dagen hebben gewerkt. Zijn nu gereset naar 0.", color=0x0ad6ff)
embed.set_author(name="Daily Firework")
embed.add_field(name="IamKiaran", value=f"{tijden[0]} minuten aanwezig", inline=False)
embed.add_field(name="GwnDaan_", value=f"{tijden[1]} minuten aanwezig", inline=False)
embed.add_field(name="hamburger7439", value=f"{tijden[2]} minuten aanwezig", inline=False)
await client.send_message(945349385997398037,embed=embed)
client.run('token')
The errors when I do !aanwezig:
1.09 18:47:50 [Bot] [2022-09-21 17:47:50] [[ERROR] ] discord.ext.commands.bot: Ignoring exception in command aanwezig
21.09 18:47:50 [Bot] Traceback (most recent call last):
21.09 18:47:50 [Bot] File "/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 190, in wrapped
21.09 18:47:50 [Bot] ret = await coro(*args, **kwargs)
21.09 18:47:50 [Bot] File "/bot.py", line 108, in aanwezig
21.09 18:47:50 [Bot] await channel.send(embed=embed, view = klokken)
21.09 18:47:50 [Bot] File "/.local/lib/python3.9/site-packages/discord/abc.py", line 1520, in send
21.09 18:47:50 [Bot] with handle_message_parameters(
21.09 18:47:50 [Bot] File "/.local/lib/python3.9/site-packages/discord/http.py", line 187, in handle_message_parameters
21.09 18:47:50 [Bot] payload['components'] = view.to_components()
21.09 18:47:50 [Bot] TypeError: to_components() missing 1 required positional argument: 'self'
21.09 18:47:50 [Bot] The above exception was the direct cause of the following exception:
21.09 18:47:50 [Bot] Traceback (most recent call last):
21.09 18:47:50 [Bot] File "/.local/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 1347, in invoke
21.09 18:47:50 [Bot] await ctx.command.invoke(ctx)
21.09 18:47:50 [Bot] File "/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 986, in invoke
21.09 18:47:50 [Bot] await injected(*ctx.args, **ctx.kwargs) # type: ignore
21.09 18:47:50 [Bot] File "/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 199, in wrapped
21.09 18:47:50 [Bot] raise CommandInvokeError(exc) from exc
21.09 18:47:50 [Bot] discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: to_components() missing 1 required positional argument: 'self'
And the errors when I start up the bot:
21.09 18:47:05 [Bot] [2022-09-21 17:47:05] [[INFO] ] discord.client: logging in using static token
21.09 18:47:06 [Bot] [2022-09-21 17:47:06] [[INFO] ] discord.gateway: Shard ID None has connected to Gateway (Session ID: 28b1801e5c111ee30170290c0aaed5bf).
21.09 18:47:08 [Bot] Kloksys. De Klok database verbinding gemaakt!
21.09 18:47:08 [Bot] Bot is currently online!
21.09 18:47:08 [Bot] [2022-09-21 17:47:08] [[ERROR] ] discord.ext.tasks: Unhandled exception in internal background task 'klok'.
21.09 18:47:08 [Bot] Traceback (most recent call last):
21.09 18:47:08 [Bot] File "/.local/lib/python3.9/site-packages/discord/ext/tasks/__init__.py", line 239, in _loop
21.09 18:47:08 [Bot] await self.coro(*args, **kwargs)
21.09 18:47:08 [Bot] File "/bot.py", line 40, in klok
21.09 18:47:08 [Bot] aanwezigtime = await db.execute_fetchall()
21.09 18:47:08 [Bot] File "/.local/lib/python3.9/site-packages/aiosqlite/context.py", line 54, in wrapper
21.09 18:47:08 [Bot] return Result(method(self, *args, **kwargs))
21.09 18:47:08 [Bot] TypeError: execute_fetchall() missing 1 required positional argument: 'sql'
Does someone know how I fix this?