I'm trying to do the rank command which I did but I don't know how to create the card image. I tried to use PIL but as I read it is dead and I have not found anything new. How could I do now? There is no tutorial with discord.py
#client.command()
async def rank(ctx, member: discord.Member):
#Qui definiamo tutte le variabili
cursor = levelsystem_db.cursor()
cursor.execute(f"SELECT user_xp FROM users WHERE client_id = {str(member.id)}")
user_xp = cursor.fetchall()
xp = user_xp[0][0]
cursor.execute(f"SELECT user_level FROM users WHERE client_id = {str(member.id)}")
user_level = cursor.fetchall()
lvl = user_level[0][0]
cursor.execute(f"SELECT xp_level FROM levels WHERE level = {lvl + 1}")
xp_level = cursor.fetchall()
next_xp = xp_level[0][0]
await ctx.send(f"Livello: **{lvl}**\nEsperienza: **{xp}|{next_xp}**")
PIL was picked up by pillow you can see the tutorial here https://pillow.readthedocs.io/en/stable/handbook/tutorial.html
Also, i just want to point that you shouldn't use f-strings to build sql queries. If you happen to parse an insecure query your whole database could be compromised.
Here's a good explanation from the sqlite3 python driver. https://docs.python.org/3/library/sqlite3.html
Here's the appropriate xkcd comic to illustrate it.
https://xkcd.com/327/
Related
Iam trying to add image user activity status in discord.py and basically, the problem is that I can't seem to add an image so I looked up discord.py API but couldn't find how to do it. So if you know do answer.
Would like to know how to add one. (NOT FOR BOTS)
Here is the code
import discord, os, keep_alive, datetime
import discord.ext
from discord.ext import tasks
intents = discord.Intents.default()
client = discord.Client()
launch_time = datetime.datetime.utcnow()
#client.event
async def on_ready():
print("Account Activated")
print(launch_time)
if not Status_loop.is_running():
Status_loop.start()
Put it in a loop to constantly update the activity
#tasks.loop(seconds=1, count=None, reconnect=True)
async def Status_loop():
delta_uptime = datetime.datetime.utcnow() - launch_time
hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600)
minutes, seconds = divmod(remainder, 60)
days, hours = divmod(hours, 24)
print(f"Online Time: {days:02d}d | {hours:02d}h | {minutes:02d}m | {seconds:02d}s")
await client.change_presence(status = discord.Status.dnd,
activity = discord.Activity(type=discord.ActivityType.playing,
large_image = "https://i.imgur.com/Kk2BvJg.jpg",
large_text = "This is Game Icon",
name = "Sleeping Simulator💤",
details = "Dreams of Desires(Easy)",
state = f"{days:02d}d | {hours:02d}h | {minutes:02d}m Passed"))
Here is the Status on a user account
If you are wondering why I am even doing this, well I just wanted a custom status
It seems that after trying sites and other kinds of ways to do it, I finally got it using replit uploaded file link... and it worked...
large_image = "https://e8a593e1-79cc-4f08-9992-2b588af90e7f.id.repl.co/static/sleepingface.jpg",
large_text = "This is Game Icon",
small_image = "https://e8a593e1-79cc-4f08-9992-2b588af90e7f.id.repl.co/static/sleepingface.jpg",
small_text = "This is Game Icon",
The result:
So I'm working on a discord bot using discord.py and I'm trying to create a bot for the moderation team in a server, the bot will swap the 'Moderator' role with a 'Leave of absence' role for when they're not active, however the code I have come up with has a slight loopholing problem that I just can't figure out, the code for the commands is this
...
#client.command()
#commands.has_role('Moderator')
async def sl(ctx, member: discord.Member = None):
if not member:
member = ctx.author
loa = ctx.guild.get_role(848032714715561985)
mod = ctx.guild.get_role(848032880709074944)
await member.add_roles(loa)
await member.remove_roles(mod)
await ctx.send("I have filed your Leave, take care, we look forward to your return!")
#client.command()
async def sr(ctx, member: discord.Member = None):
if not member:
member = ctx.author
mod = ctx.guild.get_role(848032880709074944)
loa = ctx.guild.get_role(848032714715561985)
await member.add_roles(mod)
await member.remove_roles(loa)
await ctx.send("Welcome back!")
...
as you can see anyone could use the second command to just give themselves a moderator role, I can't set the second command to be moderator only use as the moderator will no longer have said role from using the first command, I'm racking my brain to think of a work around i.e. logging the command users id to a whitelist and having only those whitelisted id's be able to use the second command, I've done many googlesearches for this but have come back with no results, any suggestions would be appreciated, please forgive that this question is a bit lengthy and I'm still very new to coding in general so any help at all, even if you don't fully understand what I'm blabbering on about would be very appreciated, thank you.
Check for the loa role in the command (ex):
mod = None
for role in ctx.author.roles:
if role.id == 848032714715561985: mod = True
if mod:
#your code here
So from your question, I'm guessing that you would like to code basically a "storage" file and make sure the person on the leave of absence was previously a moderator.
What you could do is create a csv file, for example records.csv (in the same folder as your main .py file of course), and every time someone calls the sl command, the program will record the user that used it.
import csv
#client.command()
#commands.has_role('Moderator')
async def sl(ctx, member: discord.Member = None):
if not member:
member = ctx.author
loa = ctx.guild.get_role(848032714715561985)
mod = ctx.guild.get_role(848032880709074944)
await member.add_roles(loa)
await member.remove_roles(mod)
file = open("records.csv", "w")
file.writelines(str(ctx.author.id))
file.close()
await ctx.send("I have filed your Leave, take care, we look forward to your return!")
#client.command()
async def sr(ctx, member: discord.Member = None):
if not member:
member = ctx.author
mod = ctx.guild.get_role(848032880709074944)
loa = ctx.guild.get_role(848032714715561985)
found = False
with open('records.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
if row[0] == str(ctx.author.id):
found = True
break
if found = False:
await member.add_roles(mod)
await member.remove_roles(loa)
await ctx.send("Welcome back!")
else:
await ctx.send("We do not have history of you having a moderator role.")
Are you running the program through an online environment like Repl.it? If so, this may not be the best way to approach this problem since people would have access to your this records.csv file (which you may not care about but just in case). If you are running the program through your desktop file directories, then there should be no privacy concerns.
import discord
from discord.ext import commands
botToken = "*"
client = commands.Bot(command_prefix = '*')
#client.event
async def on_ready():
print("I'm ready!")
#client.command()
async def start(ctx):
a = await ctx.guild.create_category("TEST", position=0)
a.position = 0
client.run(botToken)
position= int and channel.position = int ain't working..Haven't found any question related to my problem, gave bot Administrator permissions at https://discord.com/developers/applications and got the link let him in my server, create a role with everything allowed and gave him the role, didn't work..
create_text_channel and create_voice_channel nothing changes, The channels are being set as if I didn't assign a position to them.
Picture showing where the category position
It's not working because there's no position 0, the first position is always 1
category = await ctx.guild.create_category("TEST", position=1)
If it still creates the category in another position you can manually edit it:
await category.edit(position=1)
I'm trying to add a cooldown to a on_raw_reaction_add event in discord.py. I'm trying to do this by using cooldown mapping. The result should be to limit how many times this message
embed.description = f"It looks like you're joining us on a newly registered account. Verification requires accounts to be atleast a day old so please stick around and try again at {test_time.time().strftime(format)}."
is sent to a user if they don't meet the account age upon adding a reaction, which should be one reaction before the cooldown kicks in.
The error I get is 'RawReactionActionEvent' object has no attribute 'author not sure why this is happening.
Here is the full code I'm working with:
#commands.Cog.listener()
async def on_raw_reaction_add(self, payload): # Will be dispatched every time a user adds a reaction to a message the bot can se
general = self.bot.get_channel(701547632354525249)
introductions = self.bot.get_channel(705068046267580566)
botroom = self.bot.get_channel(706610280032894996)
if not payload.guild_id:
# In this case, the reaction was added in a DM channel with the bot
return
guild = self.bot.get_guild(payload.guild_id)
author = guild.get_member(payload.user_id)
bucket = self._cd.get_bucket(payload)
retry_after = bucket.update_rate_limit()
seconds = bucket.update_rate_limit()
seconds = round(seconds, 2)
hours, remainder = divmod(int(seconds), 3600)
minutes, seconds = divmod(remainder, 60)
if retry_after:
verification_error = f"You can do that again in **{minutes}m {seconds}s**."
embed = discord.Embed(title="You have already verified.", description=verification_error, colour=discord.Colour(0xff8100))
await message.channel.send(embed=embed)
pass
if payload.message_id != 743841858727444550:
return
else:
guild = self.bot.get_guild(payload.guild_id) # You need the guild to get the member who reacted
member = guild.get_member(payload.user_id) # Now you have the key part, the member who should receive the role
if payload.emoji.id != 743077610115956816:
reaction = payload.emoji.id
role = discord.utils.get(guild.roles, name="Members")
newbies = discord.utils.get(guild.roles, name="Newbies")
restricted_role = discord.utils.get(guild.roles, name="Restricted")
role = discord.Object(743840972068356116) # Pass the role's ID here
else:
return
if restricted_role in member.roles:
return
if member.created_at + dt.timedelta(days=30) <= dt.datetime.today():
await member.add_roles(role, reason='New member verified') # Finally add the role to the member
await member.add_roles(newbies, reason='New member verified') # Finally add the role to the member
welcome = f'Welcome {member.mention}! feel free to get started with adding some <#706648361196978177>'
intros = f'Hey {member.mention}! now that you\'re a member of our group, maybe you\'d like to tell us a bit about yourself. Tell us an interesting fact about yourself and so on... As always keep it sensible and fun.'
await general.send(welcome)
sent = await introductions.send(intros)
await asyncio.sleep(900)
await sent.delete()
else:
format = '%I:%M%p'
member1 = member.created_at.strftime("%I:%M%p")
dt3 = dt.datetime.strptime(member1, format)
test_time = dt3 + dt.timedelta(minutes = 1500)
embed = discord.Embed(colour=discord.Color(0x7289DA))
embed.title = f"Hey there {member.display_name}!"
embed.description = f"It looks like you're joining us on a newly registered account. Verification requires accounts to be atleast a day old so please stick around and try again at {test_time.time().strftime(format)}."
await member.send(embed=embed)
Help would be appreciated.
Under your #client.command statement just add
#commands.cooldown(1, 30, commands.BucketType.user)
This example will give a single user a cool down of 30 seconds for 1 use. You can configure BuckType to channel and server as well.
I'm trying to read a book name from a user after asking the question: What book are you looking for? How can I save the user's response in a variable for use in my algorithms?
def bookinfo(bot, update):
chat_id = update.message.chat_id
bot.send_message(chat_id=chat_id, text='What book are you looking for?🔎')
dp.add_handler(MessageHandler(Filters.text))
BOOK_NAME = update.message.text
BOOK_NAME = str.lower(BOOK)
answer = 'You have wrote me ' + BOOK_NAME
bot.send_message(answer)
updater = Updater('TOKEN')
dp = updater.dispatcher
dp.add_handler(CommandHandler('bookinfo', bookinfo))
updater.start_polling()
updater.idle()
The question is asked, but the bot does not respond by sending the message with the name of the book... Many thanks in advance!
at first always get chat_id from your update like this:
chat_id = update.effective_user.id
and also send_message method need a chat_id to send it you have two choice to answer to this update:
even don't need to add handler to your dispatcher
bot.send_message(chat_id, message)
update.message.reply_text(message)
def bookinfo(bot, update):
update.message.reply_text(text='What book are you looking for?🔎')
def get_bookinfo(bot, update):
book_name = update.message.text
book_name = str.lower(book_name)
# TODO: do what you want with book name
answer = f'You have wrote me {book_name}'
update.message.reply_text(answer)
updater = Updater('TOKEN')
dp = updater.dispatcher
dp.add_handler(CommandHandler('bookinfo', bookinfo))
dp.add_handler(MessageHandler(Filters.text, get_bookinfo))
updater.start_polling()
updater.idle()