'Discord Py Tempban
@loop(seconds=10)
async def tempun(client):
db_name = "database/tempban.db"
db = await aiosqlite.connect(db_name)
try:
cur = await db.execute("SELECT * FROM tempban")
fetch = await cur.fetchall()
cur_time = datetime.utcnow()
for row in fetch:
ban_time = parser.parse(row[2])
print(ban_time)
if cur_time >= ban_time:
unbanuser = int(row[1])
guilduser = int(row[0])
try:
member = discord.Object(id=unbanuser)
guild = await client.fetch_guild(guilduser)
except Exception as e:
print(e)
try:
await guild.unban(member, reason="Tempban expired")
deletetime = await cur.execute("delete from tempban where guild_id = ? AND user_id = ? returning *", (guilduser, unbanuser,))
await db.commit()
except Exception as e:
print(e)
except Exception as e:
print(e)
The Code won´t Give any Errors it does nothing it prints the datetime from a user of my database all database values are there.
Solution 1:[1]
A good way is to use the asyncio library.
import asyncio
async def tempun(client):
# get data from the database
await asyncio.sleep(seconds_to_sleep)
await guild.unban(user, reason)
# delete from database
The tempun function can be called in the ban command or wherever it needs to be triggered
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sandy |
