Create a command to update a discord bot database - python

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.

Related

Discord Bot Python Databases

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

Update preexisting telegram menus using python

I am building a telegram bot that will automatically add messages to preexisting menus in a bot ( created by Manybot, not with python ) once they are uploaded to a specific channel.
I spent a week or so trying to find a way to update preexisting menus but it can't manage to find a solution.
even when I try to run an infinite loop to monitor any messages to the bot ( using any telegram-python library out there ) i get this error :
Conflict: can't use getUpdates method while webhook is active; use deleteWebhook to delete the webhook first
and i am forced to delete the webhook , which will in turn disable every command i added to the menus and need to create another bot
I would really appreciate any help .
The problem is not related to existing webhooks, it's the fact that each bot token can poll / webhook only once.
In other words, you can't have two scripts running with the same bot token, regardless if it's long polling or a webhook.
This is a limitation of the Telegram Bot API.
You can only run your own script when the manybot version of your bot is paused, stopped, turned off, etc. The getUpdates method will reject new connections with the same bot token.
You can either use a second bot token to use other commands if your setup allows it, or use the MTProto API with frameworks such as Pyrogram or Telethon.

How to handle messages in a Telegram Bot?

My goal is to make a Telegram Bot using Python that does the following:
The user types a command
The bot explains what the user should type next.
The user types certain information
I use that info to fetch a value in a python dictionary.
The bot replies with that info
I have already created the bot and set the command. The problem is that I don't know how to keep the bot "listening" . I have looked up in the docs but I have only found getUpdates, which gets the user's responses but only when you hit run.
res=requests.get(url=f"https://api.telegram.org/bot{bot_token}/getUpdates")
I would like to set a webhook, and I know there is a method for this but I'm not shure about how this works.
When you are a PHP programmer, setting the webhook through a URL and using setWebhook would do the trick and telegram will send the result to this link whenever a user sends an update to the bot. But, Python programmers have to use a different approach, I think. The main, and while, the easiest approach to make the bot listen permanently to the request is to python-telegram-bot module.
There are a few links that can help to build your first pythonic bot that can respond to users' updates.:
Python-Telegram-Bot Repository, which is the repository of the module.
A few examples of bots created using this module, can give you insight into the process of creating your first bot.
A conversation bot

Is there a way to create a invite to only be used by a specific user? - Disnake

About: I am using Disnake in python (version 3.9.7)
Hello all,
I am trying to make a bot where bot a creates a server (since bots can only be in 10 guilds if you want to be able to create guilds), and then a discord user(me) then invites a main bot which has permission to make slash commands and contains all the moderation code.
I need to create a invite that can only be used by the user that run the command to the main server which the slash command is run in. I am currently using Disnake's disnake.ext.commands.bot rather than disnake.client

Updating Telegram Bot Commands In Realtime

Telegram allows the commands to be updated using setMyCommands. I can successfully update the commands in realtime based on the user input using a python API, pyTelegramBotAPI.
However, the problem is the user has to exit the chat with the bot and then come back to the chat again to see the new commands (by typing /).
Is there any way that I can have the bot update the list of commands in realtime with the user still in the chat?
Actually it is possible to do this real time if you set the scope to chat with specific user. There is parameter scope in set_my_commands function. By default set_my_commands affects all the chats of your bot. However, you can provide telegram.BotCommandScopeChat as a value of scope param. See the code below, it updates the command menu immediately without need to switch chats.
def _set_menu(commands: List[BotCommand], update: Update):
''' sets the chat commands menu '''
bot = Bot(os.getenv('TELEGRAM_TOKEN'))
bot.delete_my_commands()
if update:
bot.set_my_commands(
commands=commands,
scope=telegram.BotCommandScopeChat(
chat_id=update.effective_chat.id
))
else:
bot.set_my_commands(commands=commands)
So I tried it with telegram client (on linux and on android) and the commands do not change unless the user re-enters the chat. This I think is because the telegram-client only loads the commands when the user enters the chat.
But I also tried this with telegram web and found that the commands changed immidiately after I changed the command sets from BotFather. The webapp actually did load the command set without me leaving the chat, after sending a single message/command
So its definitely a problem with the telegram-client.

Categories