CodingLion
CodingLion

Reputation: 84

Discordpy timed message or ban or role, WITH bot restart | discordpy

I want to code a timed ban or mute, but with that I can restart my bot. Is there a nice libary or anyone has an idea to code it?

Thank you very much!

I code with discordpy cogs

Upvotes: 1

Views: 119

Answers (2)

Kranthos
Kranthos

Reputation: 9

If it involves restarting your bot, then you cannot use your RAM to store your data but you need to use your Hard Disk for that. When your bot is running, it is storing its data inside the RAM and that's why you can re-use them while the bot is online. Once it goes offline or gets restarted, all the data are removed from the RAM because the program is shut down.

To store those data within the Hard Disk, you need a database. For such small projects, you can use JSON or SQLite. If the project scales, you can move to another SQL like MySQL that will handle a more complex and heavy database.

To make a bot that can do a timed message:

  1. You need to store the data of when the message is going to be sent on your hard disk (database), then use that data to send that message. For example, you want to send "hello" in 1 day. That basically means that you want to send it at 8/7/2021 6:19 PM (it's 7/7/2021 6:19 PM right now). So, you store 8/7/2021 6:19 PM as a piece of data of when the bot is going to send the message.
  2. Then you make the bot compare the current time with the time that you saved on your database. If it is greater, then it will send the message and delete the data from the database. You can use the same technique with timed ban, role and everything else.

From a technical standpoint, you can use Discordpy for all the Discord stuff, datetime to check the time, JSON (or SQlite3) for the database.

Upvotes: 0

Axisnix
Axisnix

Reputation: 2907

You would have to use a database system such as MongoDB or MySQL. You will have to store the active timed ban/mute in a record/document or another table/collection. Then if you are using MySQL you would only select user's that are timed by using Tasks. In MongoDB, you just search for any users that are timed.

You would either make a separate cog or keep it in your moderation cog, it must be executed when it's ready and execute every 5-30 minutes checking if the time is older than the current time. You could use timestamps to accomplish this. Then just update the document/record once the time is up and remove their id from a ban list.

Upvotes: 1

Related Questions