Discord Bot Python Databases - python

I currently have made a discord bot that has an economy data base (in json format). However, I do not want this economy information to carry between servers (when its added to multiple servers) but instead want a new json file for each server. Can anyone tell me how a discord bot can tell if its in a new server and how to create a new json file for that said server (without me adding it to my file).
If there is other methods without making a new file for each server, I am open to hear any ideas as well :)

discord.py has a on_guild_join event you can monitor to detect new guilds. You could make a dictionary that uses the Guild ID as a key, and the JSON object as a value for example.
#client.event
def on_guild_join(guild):
guild_db = economy_db.get(str(guild.id))
if not guild_db: # If the guild hasn't been visited yet
economy_db[str(guild.id)] = dict()
# Do your stuff to create a new economic system
else:
# In case the guild already exists in the database
# Do stuff here

Related

How can I get list of users who started Telegram bot before in python?

I've seen some bot creators before with options to extract all members of bot who interacted (or started) bot before. but I can't find any method in telegram docs for extract users. Do you know any method for extract users of bot in any library or something?
I asked ChatGPT but he gives me a code for extract chat users:
async def get_users(client, message):
if message.text == "/users":
# get all users who have ever interacted with the bot
async for member in app.iter_chat_members(chat_id=message.chat.id):
user = member.user
if user.is_bot:
continue
if user.status == 'restricted' or user.status == 'kicked':
continue
How can I get UserID from user who started my bot before if I didn't save it?
there is any method for extract users of bot in any library or something?
How can I get UserID from user who started my bot before if I didn't save it?
You cant!
After you've passed an offset to the getUpdates call to remove any messages from the update queue, they're gone. Reff
There is no way to ask Telegram for a list of chat's that ever talked with your bot.
The only way of doing this is by a custom implementation where you save each chat id somewhere to recall later.

Using Discord's API to Register New Messages

What I'm trying to understand is how Discord.py creates and sends responses from their on_message function.
Take this as an example:
#client.event()
async def on_message(message):
print(message.content)
I'm trying to understand how Discord.py retrieves new messages from Discord without refreshing the channel histories for every single channel in every single server to scan for new messages which would surely hit Discord's rate limit.
Is there a way to scan for new messages with Discord's API using fetch or post requests? I am not trying get a solution on how to scan new messages using an already made library. I want to achieve this using only the requests module in python.
I'm trying to understand how Discord.py retrieves new messages from Discord without refreshing the channel histories for every single channel in every single server to scan for new messages which would surely hit Discord's rate limit.
Discord bots establish a websocket connection with Discord's servers, which is essentially a (secure) two-way tunnel. This means that once the connection is up, Discord can send events to you. Instead of you having to manually fetch every single channel, Discord tells you "Hey, a message was created", and the payload attached will give all the additional info about it.
The Discord (not discord.py) docs have detailed info about how everything works behind the scenes, to help the people creating the Bot libraries for you. For example, this section details which types of events Discord can send to you. To see how something is constructed, click on one of the event types and read up on the data that Discord provides.
Is there a way to scan for new messages with Discord's API using fetch or post requests? I am not trying get a solution on how to scan new messages using an already made library. I want to achieve this using only the requests module in python.
Not really, unless you do in fact send a GET for every single channel, which will get you ratelimited. There's really no reason to ever use only GET/POST requests (other than Webhooks, where you just send a POST with your info to send a message to a channel without a bot).
If you'd like to read up on Discord's API, the docs I linked contain a full spec of everything, so you could try to do whatever your heart desires (... and the API supports).

Create a command to update a discord bot database

I have made a discord bot in python which makes use of databases to store channel ids where my bot is running. I have a command which allows me to add/remove items from this database, however, it will not actually update the database unless if I reboot the bot. For reference, this bot is written in discord.py and is written on repl.it. This is all I have code-wise, so far:
db['channel_ids'] = channel_ids #just to show which database I want to reference
#client.command(name = 'update_database')
async def update_database():
# want this function to simply make the database update/reload, if this is at all possible.
To add more information, I have a command which allows me to see all of the channel id's which my bot is running in. However, as mentioned, the list it returns may be outdated if I have added/removed some channel ids and not yet rebooted the bot. All I want is a way to get the database to update without having to reboot the bot. Thanks all.

communication between two bots? (discord.py)

I'm a beginner-intermediate level programmer working with discord.py for the first time. I want to create two bots that, when one is prompted, both send messages one after another as if in a conversation.
Is that even possible in discord.py? I considered making two different bots in two different .py files, creating variables for each line of the conversation for both bots, and then having them each prompt if the message content matched the variable. However, I don't want the bots to prompt if the line is said by someone other than the other bot.
Any advice? Thanks so much!
You can use on_message event and check if the id of the author is the id of second bot. Example:
#bot.event
async def on_message(message):
if not message.author.id == 0000: # id of another bot
return
# rest of the code
You can use the discord-ext-ipc lib.
You can setup a server on both bots and exchange Http Messages whenever an specific Event triggered on one bot.

What is Django equivilant to Rails initializers folder?

I have a script for a discord bot that I need to use in a Django App so I can reference the DB and Models from it. The goal is to dynamically output messages to discord based on records.
In Rails with the discord gem: https://github.com/discordrb/discordrb
I am able to add my script to the initializers folder. This way it starts up and it's always listening. When something like a message or command comes through, I can dynamically update the output message based on the information from the command or message from Discord.
Here is my app\config\initializers\discord_bot.rb file:
require 'discordrb'
bot = Discordrb::Bot.new token: '<>'
bot.message(with_text: 'Ping!') do |event|
task = Task.find(1)
event.respond "Pong! #{task.title}"
end
bot.run
I can access the models from here. This is a quick example but if I wanted, I can check against the discord information I receive to dynamically respond with the bot information from the database records.
How am I able to do something similar to this in Django?

Categories