Discord.py adding a command to store data - python

Basically I have a discord bot that auto leaves servers unless its in a database. I was wondering how I could make a command to add to that database and remove? Heres my current code
server=[878537556332015626,
884958773913985054,
869615568838357052]
#client.event
async def on_guild_join(guild):
if guild.id in server:
pass
elif guild.id not in server:
await guild.leave()
So where it says "server=[stuff here]" how could i make a command to add servers ids to that database? Maybe like ';addserver server-id' and it'd add it to that database, and also a remove command. Like ';removeserver server-id' ive attempted multiple times but it didnt work, neither did it show an error.
Or help me make this into a json file!

If your goal is to make a command for your bot such as ;addserver server-id, it would be quite simple. Here's an example for checking the validity of the ID and writing it to a servers.json file if it's valid:
#client.command()
async def addserver(ctx, message = None):
if client.get_guild(int(message)): # If the bot is in a guild with this ID, returns True
serverFile = json.loads(open('servers.json', 'r').read()) # Reading Json file to a dict
serverFile['servers'].append(int(message)) # Appending new ID to the dict
with open('servers.json', 'w') as writeFile:
json.dump(serverFile, writeFile, indent=4) # Writing dict to Json file
else:
pass # Do whatever you want with this part, maybe an error message would be sent to the command sender or something.
And servers.json should be laid out like this:
{
"servers": [
]
}
Reading the list of server IDs from the file would look like this:
json.loads(open('servers.json', 'r').read())['servers'] # Returns a list of IDs in the file.
And to remove an ID from the list:
#client.command()
async def removeserver(ctx, message = None):
serverList = json.loads(open('servers.json', 'r').read())['servers']
for id in serverList:
if int(message) == id:
serverList.remove(id)
serverDict = {}
serverDict['servers'] = serverList
with open('servers.json', 'w') as writeFile:
json.dump(serverDict, writeFile, indent=4)
The part you'll likely find the most value out of is the if client.get_guild(int(message)) line, as it will let you know whether the ID is valid or not. This can be applied to new functions that could clean up your server list if need be.

Related

I don't want the Telegram bot send repeated messages

i'm learning Python!
I'm building a Bot that searches for products in an e-commerce, the bot will always send a message with a price predefined by me!
The bot does a new search every 5 minutes, but I don't want send the same product again in the next message, i did some research but found nothing that fits what i need.
Sorry for my bad english! I hope you understand what I meant.
Code:
def send_to_telegram(message):
apiToken = 'xxxxx'
chatID = '-xxxx'
bot = telebot.TeleBot(apiToken)
if placa and fabricante_1 in marca:
bot.send_message(
chat_id=chatID, text=f"<b>Modelo:</b> {marca} \n<b>Fabricante:</b> {fabricante_1}\n<b>Preço a vista:</b> R$ {valor_preco_avista} \n<b>Preço a prazo:</b> R$ {valor_preco_prazo} \n<b>Loja:</b> {loja} \n\n<b>Link Produto:</b> {url_completa}", parse_mode='HTML')
I tried some modules like: schedule, time
But in the end the bot sends the duplicate message anyway.
I managed to solve my problem, I'll leave the solution for anyone who has a similar doubt in the future!
To avoid sending repeated messages with a Telegram bot with Python, you can store a record of the messages that have already been sent and check against this record before sending a new message.
import os
import pickle
sent_messages_file = "sent_messages.pickle"
if os.path.exists(sent_messages_file):
with open(sent_messages_file, "rb") as f:
sent_messages = pickle.load(f)
else:
sent_messages = []
def send_message(mensagem):
apiToken = 'XXXXXXXXXX'
chatID = 'XXXXXXXX'
bot = telebot.TeleBot(apiToken)
bot.send_message(
chat_id=chatID, text=message, parse_mode='HTML')
sent_messages.append(message)
with open(sent_messages_file, "wb") as f:
pickle.dump(sent_messages, f)
In this code, the sent_messages list is saved to a file using the pickle module. Before sending a new message, the code checks if the sent_messages_file exists. If it exists, the code loads the sent_messages list from the file. If it does not exist, the code creates an empty sent_messages list. After sending a message, the code saves the updated sent_messages list to the file.
This way, when you run the code again, it will load the sent_messages list from the file, and only send new messages that have not been sent before.

Discord Bot checking if key from json present in message

from discord.ext import commands
import json
with open(r"C:\Users\intel\Desktop\rijalbot\rijaldata\narrators.json") as f:
narrators = json.load(f)
class cmds(commands.Cog):
def __init__(self, client):
self.bot = client
#commands.command()
async def rijal(self, ctx, message):
if narrators.keys() in ctx.message:
val = narrators.get(ctx.message)
if val == "weak":
await ctx.send("True")
narrators.json
{"a": "weak"}
I want my discord bot to check if my message contains a key from a json, but everytime I run it, and execute the [!rijal a] command, it does nothing, it should send "True"
narrators.keys() is a view of all keys in the dictionary. The message is a string, so narrators.keys() will never be in message.
narrators.get(message) also won't work if the message isn't exactly the same. You're using in, so you're just looking for substrings. For example: "a" is in "another", but {"a": "weak"}.get("another") won't find a match, because "another" isn't inside of the dictionary.
If you want substrings, loop over the keys instead. To get the value, get it from the dict using the corresponding key instead of the message, as that won't work (as described above). An alternative is to loop over both the keys & values at the same time using items.
If you want full matches, get() will return None if nothing was found.
PS consider using an actual database instead of a JSON file

How do I get the user ID of the person who issued the command in Discord

So im making a discord Bot that will notify me when my favorite food is avalable in the cafiteria at my school. Im trying to make it so multiple people can use the bot so im storing the users favorite food in a dictionary with their user id as the key. I've never used slash commands in discord before, normally I just use a command.prefix but decided to try it this way instead.
So far this is what I have:
client = aclient()
tree = app_commands.CommandTree(client)
#tree.command(guild = discord.Object(id=guild_id), name = 'favorite_add', description='Add a food item to your favorite food list') #guild specific slash command
async def addFavorite(interaction: discord.Interaction, content : str = ""):
await interaction.response.send_message(f"{content} has been added to your favorite food list", ephemeral = True)
userFavorites[content.author.id] += [content]
The problem im having is that the str: content doesnt have the author.id attrubite. Is there any way to aquire the id of who posted the command so I can add the content to their favorite food list or potentially add a new key to my dictionary
async def addFavorite(interaction: discord.Interaction, content : str = ""):
^^^^^^^
That's because the content argument is a string since you're telling the library that you wanted it to be. If you want to get the ID of the user who triggered the command, you have to get the user from interaction.user then get the ID.
userFavorites[interaction.user.id] += [content]
discord.py isn't beginner-friendly; you should know the basics before trying it. You can look at some examples here.

Discord.py 2.0 on Repl.it

I want to make discord bot that uses buttons I downloaded library and ewerything. It works but because I am using Repl.it i have to use web + uptimerobot so it will never turn off.
Even after that it seems every few hours to quickly turn off and back on, so it will lose all data except databases. I was thinking i can solve this by saving message to database and inside event on_ready delete message and create new one but bot after going off and on cant delete message from varriable.
Code for web:
from flask import Flask
from threading import Thread
app = Flask('')
#app.route('/')
def home():
return "I am working!"
def run():
app.run(host='0.0.0.0',port=8080)
def keep_alive():
t = Thread(target=run)
t.start()
I would like to solve turning off and on problem + it will be helpfull to fix deleting message from database after reseting.
Side question: Do you guys know any easy way to assign instead of reference value to varriable because of circular reference/dependency.
Make a JSON file to save the data and just have it not delete the data on restart. have it autosave when a user uses it.
kinda like this
#client.command()
async def add(ctx, meme_):
def add_memes(meme, file="memes.json"):
with open(file, "r+") as fw:
j = json.load(fw)
j["memes"].append(meme)
with open(file, "w+") as wp:
wp.write(json.dumps(j))
try:
with open("memes.json", "r"):
pass
except:
with open("memes.json", "w+") as wp:
wp.write('{"memes" : []}')
finally:
add_memes(meme_)
await ctx.send("Done!")
but this is a command for saving memes

Command unblocker

I have this cog blocker that puts a guild id and the command blocked in a .json file. When the command is used, it will check to see if this information is in the .json file, if it is, it will not run the command. Although, I do not know how to make it so it takes out the command or guild id so the command can be used again. Please help!
#commands.command()
#has_permissions(administrator=True)
async def block(self, ctx, cmd: str):
with open(os.path.dirname(__file__) + f'/json/data.json','r+') as f:
data=json.load(f)
if not str(ctx.message.guild.id) in data:
data.update({str(ctx.message.guild.id): []})
data[str(ctx.message.guild.id)].append(cmd)
else:
if cmd not in data[str(ctx.message.guild.id)]:
data[str(ctx.message.guild.id)].append(cmd)
await ctx.send(embed=discord.Embed(description=f'{ctx.author.mention} has banned the command **{cmd}**',color=65535))
else:
await ctx.send(embed=discord.Embed(description=f'{ctx.author.mention} **{cmd}** is already banned', color=65535))
self.write("data", data)
If you can give me the code, that would be greatly appreciated.
Ok so you can remove the guild id simply by
# Do a if id in data.keys() b4 to make sure there wont be a KeyError
with open('json/data.json','r+') as f: # u don't need the f"" or __file__
data = json.load(f)
del data[str(ctx.guild.id)]
# save json do bomb stuff
Now deleting a specific command
#Load json like the previous snippet
if cmd in data[str(ctx.guild.id)]:
del data[str(ctx.guild.id)][data[str(ctx.guild.id)].index(cmd)]
#save json
and its also advisable to use a db instead of json(its very useful for seldom changed data) but if its a small project json would do fine

Categories

Resources