#Basic Pycord Help
1 messages ยท Page 46 of 1
so store it at a local file ;3
Can I actually upload images passing a BytesIO object? It doesnt work on my end. it uploads an image with 0b. and this happens for every valid image I try it on.
await discord_channel.send(file=discord.File(photo_in_bytes))
this works thanks! How can free the ram since I dont need the image stored on the ram anymore?
we aren't in C lol, no need
actually show the code to open the file i forgor
as long as you use with you're fine
I dont actually open it, im developing a bot that forwards posts from a personal telegram channel to a discord channel. Idk if youre familiar with pyrogram library, but basically it fetches the latest 5 posts from a telegram channel, and for each one Im downloading the image in bytes:
@discord_bot.event
async def on_ready():
print(f"{discord_bot.user} is ready and online!")
async with Client('telegram_user1', session_string=session_string1) as app: #log into telegram account using pyrogram
posts = app.get_chat_history(telegram_channel_id, limit=5) #get latest 5 posts
async for post in posts:
if post.media.__str__() == "MessageMediaType.PHOTO":
post_bytes = await post.download(in_memory=True) #download image to ram in bytes
post_bytes.seek(0)
discord_channel = discord_bot.get_channel(DISCORD_CHANNEL) #send to discord channel
await discord_channel.send(file=discord.File(post_bytes))
So at least on my code theres no "with" statement apart from the one I need to use to log in, but that's permanent, as Im never logging off within the code
Yea no thats fine lol, itll be cleared by python
its just normal data in a variable, all variables get deleted from memory when they aren't in scope anymore
actually wait
i may be stupid
so basically once the for loop iteration is done, python clears off the bytes? didnt know about that
see this is why i hate file stuff its always messy af
yea I'm actually not sure anymore, sorry im tired lol
i mean can't hurt to do post_bytes.close()
or maybe you can do async with await post.download... even
both could be valid solutions, let me try with the second one
File, line 35, in on_ready
async with await post.download(in_memory=True) as post_bytes:
TypeError: '_io.BytesIO' object does not support the asynchronous context manager protocol
f
might as well try the first one xD
thought it'd say that
if you wanna make 100% sure put the send in a try except and put the close after that
close in the finally then
like this?
try:
discord_channel = discord_bot.get_channel(DISCORD_CHANNEL)
await discord_channel.send(file=discord.File(post_bytes))
except Exception as send_exc:
print(send_exc)
post_bytes.close()
It wont do much tho, will it?
.get_x
Any function that starts with get_ in Py-cord is retrieving the related object from your bots cache. If the object is not in the bots cache the get_ method will return None. Because of this behavior you should check if the get_x method is None and if it is use the fetch_x method.
Why Is Using fetch_ Without Using get_ First Bad?
The fetch_ method makes a call to the discord API. This API call is unneeded if you already have the information. It will also make your command take longer because it will have to send and than wait for a response from the discord API. It will also contribute to the discord APIs global rate limit of 50 requests per second.
What Is Cache?
The cache is a temporary storage inside your bot. It holds many objects from members to messages. When you restart your bot the cache will be empty. When the cache is full it will delete older objects to make space for the new objects.
put it in finally:
try:
discord_channel = discord_bot.get_channel(DISCORD_CHANNEL)
await discord_channel.send(file=discord.File(post_bytes))
except Exception as send_exc:
print(send_exc)
finally:
post_bytes.close()
it throws no errors
yea that looks good
I have 1 braincell gonna read in quickly
no
lmao
Great thanks again! I still dont get how this guarantees that the ram is freed after its been sent. Like wouldnt it be the same if I closed it right after the send, with to try/except/finally block?
if post.media.__str__() == "MessageMediaType.PHOTO":
post_bytes = await post.download(in_memory=True) #download image to ram in bytes
post_bytes.seek(0)
discord_channel = discord_bot.get_channel(DISCORD_CHANNEL) #send to discord channel
await discord_channel.send(file=discord.File(post_bytes))
post_bytes.close()
if the send throws an exception it will never close
Uh
yes voxy i am sorry lmao
You fucking ignore my question in dm and just show up here to tell me "no" ๐๐

xd
my fault 
files in ram
What's so difficult ab that
discussing about making sure that python frees up its ram after download an image to ram
man im stupid and tired
Ohhhhhhhhhhhh
so tldr Toothy said, just do file.close()
file.close?
I probably shouldn't question any further.
do you not use with open()?
not possible, we tried
nope cuz im downloading it
^ with open file as f
what, is it wrong
its await tho
read the chat its actually pretty short
its await download
and it says IOBytes dont support async context manager stuff
(you need async with for await in the expression right)
yeah now that im thinking about it probably I dont need async with, but just with await post.download()...
lemme try
yea i didnt think that would work
i love how it feels like we are all running on a combined 3 braincells here
everyone is confused
I- I want to say something but I'm A. Confused B. too afraid to pass false info
it literally is an IOBytes object thats returned
oh
me from the start but i just wanted to say something and not leave them stranded
with open("file", "wb") as f:
this is their og code
if they are bytes you can just do this
Zervy
actually it does
wtf
it sends the image perfectly and throws no errors
cuz you told me to
no im doubly confused but im good now
What the what
Ich bin seit 3 Uhr wach...
SEIT*
mimimi
Ok so async with is with context manager while normally when you await post.download it downloads it directly to your memory.
Use async with when you need to manage the setup and teardown of resources explicitly.
For simpler cases where such management is not necessary, using await is and should work perfectly fine.
I hope I got that right
๐
but yea, if you have with you dont need to worry about file.close()
with handles everything
import discord
from discord.ext import commands
from datetime import datetime
from data.checks import getWarnings, removeWarning, is_staff
async def get_warn_choices(ctx: discord.ApplicationContext):
target_id = ctx.target_id
warnings = getWarnings(target_id)
return [
discord.OptionChoice(name=f"{warn['id']}: {warn['reason']}", value=str(warn['id']))
for warn in warnings
]
class RemoveWarn(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.slash_command(name='removewarn', description='Remove a warning by ID')
@is_staff()
async def removewarn(
self,
ctx: discord.ApplicationContext,
target_id: int,
warn_id: discord.Option(
str,
description="Select a warning ID to remove",
required=True,
autocomplete=get_warn_choices()
)
):
removeWarning(target_id, int(warn_id))
embed = discord.Embed(
title="๐ก๏ธ Warning Removed!",
description=f"**User**: <@{target_id}>\n**Warn ID**: `{warn_id}`\n**Time Of Removal**: `{datetime.utcnow().isoformat()}`\n**Removed By**: {ctx.author.mention}",
color=discord.Color.red(),
timestamp=datetime.utcnow()
)
await ctx.respond(embed=embed)
def setup(bot):
bot.add_cog(RemoveWarn(bot))
Why wont the warns load im not getting a error and on the option i have a Call expression not allowed in type expressionPylancereportInvalidTypeForm
The grab For The errors are
def getWarnings(userID):
profile = getProfile(userID)
if profile and "warnings" in profile:
return profile["warnings"]
return []
and the warns db be looking like
{
"users": {
"769389293658570752": {
"warnings": [
{
"id": "WARN-f0ed38123e23",
"reason": "test",
"warned_by": "osf0",
"date": "2024-07-07T06:27:06.174501"
},
{
"id": "WARN-d950b8a6d258",
"reason": "test2",
"warned_by": "osf0",
"date": "2024-07-07T06:27:34.439806"
}
]
}
}
}
@errant trout
the autocompletion
ignore it
im prolly just being dumb but is there a way to add an option to a channel select? heres the code i wanna add it to
class view_class(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.channel_select(
placeholder="Level Up Channel",
channel_types=[discord.ChannelType.text]
)
async def channel_select_callback(self, select:discord.SelectOption, interaction):
nope
really-
yeah
thats annoying
channel, role and user select are completely autofilled from your guild
best method is to just use string select with channel IDs as the value
Task exception was never retrieved
future: <Task finished name='Task-18' coro=<ApplicationCommandMixin.on_application_command_auto_complete.<locals>.callback() done, defined at C:\Users\jarex\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py:869> exception=AttributeError("'AutocompleteContext' object has no attribute 'user'")>
Traceback (most recent call last):
File "C:\Users\jarex\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py", line 872, in callback
return await command.invoke_autocomplete_callback(ctx)
File "C:\Users\jarex\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 1036, in invoke_autocomplete_callback
result = await result
File "C:\Users\jarex\Downloads\Jarexs Tools\Heroic Productions\Main\commands\removewarn.py", line 7, in get_warn_choices
target_id = ctx.user.id
AttributeError: 'AutocompleteContext' object has no attribute 'user'
i changed it a bit to @ the user and not make it a id and now i get the error
code :
import discord
from discord.ext import commands
from datetime import datetime
from data.checks import getWarnings, removeWarning, is_staff
async def get_warn_choices(ctx: discord.ApplicationContext):
target_id = ctx.user.id
warnings = getWarnings(target_id)
return [
discord.OptionChoice(name=f"{warn['id']}: {warn['reason']}", value=str(warn['id']))
for warn in warnings
]
class RemoveWarn(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.slash_command(name='removewarn', description='Remove a warning by ID')
@is_staff()
async def removewarn(
self,
ctx: discord.ApplicationContext,
user: discord.User,
warn_id: discord.Option(
str,
description="Select a warning ID to remove",
required=True,
autocomplete=get_warn_choices
) # type: ignore
):
removeWarning(user.id, int(warn_id))
embed = discord.Embed(
title="๐ก๏ธ Warning Removed!",
description=f"**User**: <@{user.id}>\n**Warn ID**: `{warn_id}`\n**Time Of Removal**: `{datetime.utcnow().isoformat()}`\n**Removed By**: {ctx.author.mention}",
color=discord.Color.red(),
timestamp=datetime.utcnow()
)
await ctx.respond(embed=embed)
def setup(bot):
bot.add_cog(RemoveWarn(bot))
oki thanks ill do that
ctx.interaction.user
(maybe we should add some shortcuts there...)
i just get this
sorry elaborate
well, i'm just guessing
try using prints in getWarnings to see what results you're getting; my assumption is perhaps your getProfile function needs a string instead of an int
just gotta debug the process
Is there a way to know how many people use User-Install?
At the moment, I don't think so
Should I use Member.status or Member.raw_status?
I tried boath, but they give me the same, and the docs don't rl say what the difference is
and another question: what is the status "Invisible" for?
there is no way it is in Member.status right?
Invisible = offline
I doubt you can see it, that would defeat the entire point
status uses the Status enum, while raw_status is the string value
And invisible is considered equal to offline when checking another user, there's no differentiation
Yea, that what I thought, but I don't see a situation where I would be able to receive the Invisible since from how I se it, it is just client side (maybe hidden server side)
The authenticated user can see themselves as invisible
i.e. bot.change_presence > printing ctx.me.status would work
I'm doing a spam filter thing for my bot uh
I have a list of messages
how do I get a message object by its id
.rtfm get_message
o h
another question, why do you only have an ID?
I'm storing the messages in a json thing
why not store it in memory
because I hate doing anything the right way
holy same
why is this doing the shart and saying TypeError: 'Message' object is not subscriptable
or do I need to assign it to a variable
or
braim
not
work
show the full error
await coro(*args, **kwargs)
File "D:\Code\Python\VSGolem-Redux\VSGolem\main.py", line 26, in on_message
await bot.get_cog("spamDetect").detectSpam(message, bot)
File "D:\Code\Python\VSGolem-Redux\VSGolem\cogs\moderation\spamDetector\spamDetector.py", line 21, in detectSpam
await bot.get_message(message["messageid"]).delete()
TypeError: 'Message' object is not subscriptable```
i think you have the wrong variable there
message is your actual message object
wouldnt you just do message.delete() at that point
uh
lmao
wouldnt that delete the older message
wouldnt you wanna delete the most recent duplicated message and not the oldest
Is there a way to know if the command is ran through user installed app or as a regular command?
authorized_integration_owners
or something like that, definitely authorized_
in ctx
Not that
quick question, say in my main.py file, I have an on_message listener, which has some logic. Say I were to attach a cog, which also listens to on_message with different logic. Would the two interfere with each other, or would it work? The desired behaviour is for both logic to be executed
They can all run
As long as you don't reuse the same function name in the same class or something
alright, what do you mean by same function name? As in command name? But that won't affect listeners, right?
function name of the listener i assume
isnt there some way to get author of DeletedReferencedMessage
.rtfm DeletedReferencedMessage
i dont see any way to get the author here
why do you need that specific class
actually i was doing something like this :
# If the message is a reply, add the reference to the response
if message.reference is not None and message.reference.resolved is not None:
resolved_message = message.reference.resolved
if isinstance(resolved_message, discord.DeletedReferencedMessage):
# Reference is a deleted message
author = resolved_message.author.mention #THIS IS CREATING ISSUE
reference_line = f"**In reply to:** {author}`{resolved_message.id}`(Deleted Message)\n"
response = reference_line + response
elif isinstance(resolved_message, discord.Message):
jump_url = resolved_message.jump_url
author = resolved_message.author.mention
reference_line = (
f"**In reply to:** {author} [Jump to referenced message]({jump_url})\n"
)
response = reference_line + response
like if the message is a reply to some message, and the parent message also get deleted then ill have a context who was the author of the parent message
Hi (DeletedReferencedMessage) I want author of this message
-> How are you? (replied message)
Just a general python rule, don't name two different things the same or it overrides
how can i add a self. to a class of the bot ,
class Lumabot(bridge.AutoShardedBot):
def __init__(self, *args, **kwargs):
super().__init__(intents=discord.Intents.all(), *args, **kwargs)
self.help_command = None
self.owner_id = OWNER_BOT
self.command_prefix = DEFAULT_PREFIX
self.shard_count = 1 if is_custom_bot() else 4
self.allowed_mentions = discord.AllowedMentions(
users=True, roles=True, everyone=True, replied_user=True
)
self.multicog_metadata: Dict[str, str] = {}
what
2024-07-14 15:12:28 [WARNING] : Failed to load extension Slashs.XP.voice.set: Extension 'Slashs.XP.voice.set' raised an error: AttributeError: type object 'Lumabot' has no attribute 'multicog_metadata'
trying to be able to do in a decorator self.multicog_metadata = ...
@Lumabot.group("antiraid")
@Lumabot.command()
im just dumb that okay, i was using a classmethod and poassingf it inside the init
Im facing an issue about the prefix group since 2.6, usually if i create a group bot, i can pass a funct that will be trigger if it doesnt find the subcommand, for example +bot test will trigger the funct of bot since test is not a subcommand
however since 2.6, im facing an issue that does not trigger the funct
any idea ?
group = commands.Group(
name=slash_group.name,
description=slash_group.description,
invoke_without_command=True,
func=self.fake_coro,
checks=slash_group.checks,
)
you need the full traceback, but also if you're hacking through library features to fit your own usage then it's pretty out of scope for us
(and from what i can tell we didn't make any particular changes to commands extension)
well i did not change the code from 2.5 to 2.6 for this part, i will try to print the whole traceback
Traceback (most recent call last):
File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/core.py", line 180, in wrapped
ret = await coro(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/container/Utils/client.py", line 355, in fake_coro
description = command.description_localizations.get(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: '_MissingSentinel' object has no attribute 'get'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/container/Utils/client.py", line 390, in invoke
await ctx.command.invoke(ctx)
File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/core.py", line 1555, in invoke
await super().invoke(ctx)
File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/core.py", line 959, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/core.py", line 189, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: '_MissingSentinel' object has no attribute 'get'
ok, so i think you can solve it now
so i suppose this mean command.description_localizations return a Missing, however when i print this one its not missing
but return a real thing
well, at some point one of them is missing
i will try to debug more
fake_coro is your own function, just add some check and continue
how can I remove the link preview when sending a message with a bot?
surround the link with <>
<link>
i have gone in the wrong direction, some small print has been hidden by other error so i was thinking it was not triggering at all
rip
groups was causing the issue
Does buttons have some sort of exit code? I have some views with a lot of buttons and they work but discord says Interaction failed
buttons timeout after sometime by themselves
I mean when I click the button can I somehow tell discord that it succeed?
you have to respond to the interaction using some type of interaction.response
Ok thanks
thanks a lot man
yo @sage tendon you remember this? Do you know how it would be possible to make the discord bot detect the posts in real time? noticed that the code works just if you run it after the telegram posts have been sent, if you send posts while the bot is running, it doesnt send anything. Thanks in advance man โค๏ธ
I never did anything with the telegram API so no
gotcha, grateful anyway :)
how do you... use user-installed application commands
I've authorised the bot and checked permissions, but none of the commands show up, and they arent debug scoped or anything
are the commands correctly registered?
They're showing up as regular guild commands in my servers, and in my DMs
I can't find anything on the docs site though so im not entirely sure what im doing
can you show me what one of your commands look like?
as in code or UI? or both
code
sure two secs
@commands.user_command(name="User Info")
async def user_info(self, ctx: discord.ApplicationContext, user: discord.User | discord.Member):
await ctx.defer(ephemeral=True)
embeds = await self.get_info(user)
await ctx.respond(embed=embeds["Overview"], view=GenericLabelledEmbedView(ctx, **embeds), ephemeral=True)
@commands.slash_command(name="user-info")
async def user_info_slash(self, ctx: discord.ApplicationContext, user: discord.User):
"""Fetches information on a user or member on discord."""
if ctx.guild:
try:
user = await ctx.guild.fetch_member(user.id)
except discord.NotFound:
pass
await self.user_info(ctx, user)
These are two of my commands
I haven't changed anything since 2.5 for what its worth, I merely bumped the pycord version lol
Pycord, a maintained fork of discord.py, is a python wrapper for the Discord API - Pycord-Development/pycord
ah cheers
Im already using a decorator from another lib, but I need to send a message from my discord bot to a discord channel while inside of this other lib's decorator block. How can I do that since I can only see a way of getting the bot working under its own discord_bot decorator? Is there a way to get the bot working without using its @discord_bot.event decorator?
You cannot use multiple discord bot libs at the same time. It will break things
if they have the same import name
there is also no reason to use multiple libs. Py-cord can do all you need
and i assume the other lib can do all you need as well
hmm
can you elaborate on what you're trying to accomplish here
@bot.slash_command(name="removenote", description="Remove a moderation note from a user")
@discord.default_permissions(manage_channels=True)
async def removenote(ctx, noteid: int):
Is this the correct way to hide commands for this permission? My role hasn't been able to see the command, and I'm not sure where I went wrong.
(I've gotten the @discord.default working for all other tiers of my moderation team but I've been unsuccessful to get this permission working. People with the Administrator permission can see the command.)
could you check if the permission matches in settings > integrations > select your bot and find the command
It doesn't look like it.
click view
so what's the issue you're running into on your end
The people with the manage channel permissions are unable to see the command.
It's linked to a role called "Senior Officer" which does have the permission, but they are unable to run it or anything like that.
a little silly, but could you perhaps try renaming the command and restarting the bot?
Wait actually, quick question. If the channel that they were trying to run the command in had the permission specifically disabled, so the role is yes but the channel blocks them, would that influence it?
yes
the default permissions are exactly that, defaults - ANYTHING else will override them
Gosh dang it
The server owners have terrible permissions setup lol
Thanks for the help
allgood
Is it possible to make the interaction.respond() message only visible to interaction.user?
ephemeral=True
Thanks
embed.set_image(url=image_url)
can i use this with an attachment in the command? photo=discord.Attachment
attachment got a .url format
.rtfm discord.Attachment
discord.Attachment
discord.Attachment.content_type
discord.Attachment.description
discord.Attachment.ephemeral
discord.Attachment.filename
discord.Attachment.height
discord.Attachment.id
discord.Attachment.is_spoiler
discord.Attachment.proxy_url
discord.Attachment.read
discord.Attachment.save
discord.Attachment.size
discord.Attachment.to_dict
discord.Attachment.to_file
discord.Attachment.url
discord.Attachment.width
discord.Message.attachments
discord.InteractionMessage.attachments
discord.WebhookMessage.attachments
discord.SlashCommandOptionType.attachment
is there a way to respond to ctx without sending a message, a modal.... etc.
i don't want the The application did not respond message
yea, you have to respond
hmm well then, my approach to this feature isn't correct
i want to make the bot's icon dynamic when responding
for what use case
depending on the message, my idea was to use webhooks
well just a little more good looking
id consider a bot that constantly changes pfps to be more confusing than anything tbh
and i dont think thats possible without using webhooks
like so
i mean the embed thumbnail is enough imo
yeh but the pfp tips me off not syncing for some reason
the only, limited way you could achieve this thats kinda ugly is by responding, then responding to that message with a webhook (no clue if thats even possible), and then deleting the original response
but thats limited and you cant do it ephemerally
yea
as i said, imo, the embed thumbnail is enough
Is there a way to put a cooldown on a command based on the role of the user executing it?
ok third question
how should i add a delay for specific amount of time. And by that i want a convinient way for the user to put their delay
wish there was a datetime component for discord that would pop a UI to pick dates as well as delays and stuff
like yes i can add some format like weeks-days-hours-minutes-seconds but this feels inconvenient
yes
@commands.cooldown
i'd just do delay in seconds
hmmm
they kinda need to have a calculator open to calculate the seconds for their time
sounds simpler tho
Still not super convenient but a start is strptime
https://www.programiz.com/python-programming/datetime/strptime
well i want duration
for dates i can use something like <t:1721069524:F> <t:1721069524:F>
True. Here is a solution. I think with this it allows you to do 5H and not 5H0M0S which is a bit more user friendly
Really silly question but is it here I saw an example being able to create an embed with x information and then be able to use reacts to add "additional pages"?
Pagination, there's a guide on it
I thought there was, sound thank you.
I wrote a bot in .NET for Discord sometime ago (years ago). You were able to log exceptions/info/debugs directly to a web hook so you could obviously post the messages to a channel instead of to log file (which i currently do) does pycord have any functionality to do this? (Or any other library for that matter)?
you mean only webhooks that you create with a url?
.rtfm webhook
discord.Webhook
discord.Webhook.auth_token
discord.Webhook.avatar
discord.Webhook.channel
discord.Webhook.channel_id
discord.Webhook.created_at
discord.Webhook.delete
discord.Webhook.delete_message
discord.Webhook.edit
discord.Webhook.edit_message
discord.Webhook.fetch
discord.Webhook.fetch_message
discord.Webhook.from_state
discord.Webhook.from_url
discord.Webhook.guild
discord.Webhook.guild_id
discord.Webhook.id
discord.Webhook.is_authenticated
discord.Webhook.is_partial
discord.Webhook.name
There is not by default, however you can easily create your own logging handler
Found something that may suit ones needs. https://pypi.org/project/python-logging-discord-handler/
That's along the lines I guess. It's a bit old but may work, sorry for bothering people.
U dont bother anyone
And thanks u for that
.rtfm badge
can we get the list of the available badge of discord ?
and get the badge of a user
its in the flags
thanks
How do i create something like this?
Pycord, a maintained fork of discord.py, is a python wrapper for the Discord API - Pycord-Development/pycord
Thank you! I'll try this
is there any solution when a bot does not connect? this happens sometimes idk why
try restarting your computer
is there no alternative to having my command defer, send ephemeral error, but non ephemeral for completed command?
i try using ctx.send for the final message but the bot is stuck thinking
You need to do some sort of response before following up to non ephemeral
You coulddd try defer -> ctx.delete?
so basically send something beforehand and then just delete it
You can delete the defer without sending anything else
oh i see, ok i will try it, thank you
i could use pytimeparse
also
duration_secs = pytimeparse.parse(duration)
if duration_secs is None and duration != "":
await failure_embeds.generate_invalid_duration(ctx)
return
delta = None
if duration != "":
delta = datetime.now() + timedelta(seconds=duration_secs)
if duration_secs > 604800:
await failure_embeds.generate_higher_than_week_embed(ctx, delta)
return
await target.timeout(delta, reason="User Has Been Muted")
await target.add_roles(mute_role, reason="Member Muted")
in await target.timeout(delta, reason="User Has Been Muted")
for some reason when i look on the remove timeout even tho i putted 1H which is 1 hour
and it was 3600 seconds which does equal to 1 hour
it showed 4 hours instead
and for 3H it made it 6 hours
realised my mistake
i didn't use UTC time
is there a util to covert an object like discord.Member into json?
Since it can not JSON serializable
TypeError: Object of type Member is not JSON serializable
create a class and return it as a json
yea I could do that, but I don't rly understand why I have to recreate all the classes
It's pretty annoying and does not feel right
but why do you want a member as a json?
save only is id and then get the member using the id
I want to save a backup of a member in that moment
do a dict like so :
member = {id: id, name: name, icon: icon }
also you should take a look at that
.json
JSON is a convenient and easy-to-read data storage protocol that's widely accepted by most programming languages. However, we caution against its use for storing and retrieving data in an asynchronous environment like a Discord bot. Donโt use json!
- It's a file-based data storage, which makes it vulnerable to race conditions
- You'll need to implement your own synchronization primitives to avoid corrupting data
- If you're not careful, you could accidentally wipe your entire JSON file.
Solution? Use a database. Recommended schema are SQLite, PostgreSQL, and MongoDB.
- Async libraries exist on pypi for each of these
sqlite --aiosqlite(or Danny's wrapper: asqlite)
postgresql --asyncpg
mongodb --motor - Databases organize your data into tables, and are fast at inserting, retrieving, and removing records
- You can impose uniqueness constraints to ensure against duplication
- The Python libraries enforce synchronization for you
- The query language is intuitive, you can get running with simple queries in just a few hours!
hm, ok I will think about it
Sorry I had to specify that the decorator from the other library Im using is not a discord one. It's related to telegram userbots. Im trying to forward incoming telegram channel posts to a discord server
yeah so Im trying to forward incoming telegram channel posts to a discord server. The telegram library Im using to get incoming posts is pyrogram, and I need the decorator they have for that. But since you cannot use two decorators at the same time without breaking anything, I asked how can I make my discord bot usable (able to send a message to a discord channel) without the need of a decorator.
await bot.get_channel(id).send()
Is this alongside your own running discord bot, or what?
You don't need a decorator to send messages, so long as your bot object exists you can follow the example toothy provided above
Hi, can someone help me, I want the original message to be deleted when I press True, because the way I have done it now, it can't find the message
import discord
import json
import re
import datetime
from discord.ext import commands
from discord.commands import slash_command
from discord.ui import Button, View, Select, Modal
from datetime import datetime
class Test(commands.Cog):
def __init__(self, bot):
self.bot = bot
@slash_command()
@commands.cooldown(1, 10, commands.BucketType.user)
@commands.has_permissions(administrator=True)
async def embed(self, ctx):
view_before = View()
view_before.add_item(Create())
return await ctx.respond(view=view_before, ephemeral=True)
def setup(bot):
bot.add_cog(Test(bot))
class Create(discord.ui.Button):
def __init__(self):
super().__init__(
label="Create",
style=discord.ButtonStyle.green,
custom_id="interaction:create",
emoji="โ๏ธ"
)
async def callback(self, interaction: discord.Interaction):
await interaction.response.defer()
embed = discord.Embed(description="Test Embed")
await interaction.edit(embed=embed, view=TemplateActionsView("Test Place"))
class TemplateActionsView(View):
def __init__(self, placeholder):
super().__init__()
self.select_callback.placeholder = placeholder
self.message = None # Nachrichtenspeicher hinzufรผgen
options = [
discord.SelectOption(label="Delete Template", description="", emoji="โ", value="template_delete_rename"),
]
@discord.ui.select(row=2, options=options, custom_id="template_action__select")
async def select_callback(self, select, interaction: discord.Interaction):
if "template_delete_rename" in interaction.data['values']:
# Nachrichtenspeicherung hinzufรผgen
self.message = interaction.message
print(f"Stored message ID: {self.message.id}") # Debug-Ausgabe der Nachricht-ID
await interaction.response.send_message(
f"Do you want to delete?",
view=TemplateDeleteView("True", "False", self.message),
ephemeral=True
)
# HEre, when i click on the Button The main message must be delte
class TemplateDeleteView(View):
def __init__(self, true_label, false_label, message):
super().__init__()
self.true_button.label = true_label
#self.false_button.label = false_label
self.message = message # Nachrichtenspeicher hinzufรผgen
print(f"TemplateDeleteView initialized with message ID: {self.message.id}") # Debug-Ausgabe der Nachricht-ID
@discord.ui.button(style=discord.ButtonStyle.green, custom_id="delete_template_view_true")
async def true_button(self, button, interaction: discord.Interaction):
# รberprรผfen, ob die Nachricht existiert
if self.message:
print(f"Attempting to delete message ID: {self.message.id}") # Debug-Ausgabe der Nachricht-ID
try:
await self.message.delete()
print("Message deleted successfully")
except discord.NotFound:
print("Message not found or already deleted")
else:
print("Message reference is None")
embed = discord.Embed()
embed.description = f"The was deleted."
await interaction.response.edit_message(content=None, embed=embed)
@slash_command()
@commands.cooldown(1, 10, commands.BucketType.user)
@commands.has_permissions(administrator=True)
async def embed(self, ctx):
view_before = View()
view_before.add_item(Create())
return await ctx.respond(view=view_before, ephemeral=True)
shoud be
@slash_command()
@commands.cooldown(1, 10, commands.BucketType.user)
@commands.has_permissions(administrator=True)
async def embed(self, ctx):
return await ctx.respond(view=Create(), ephemeral=True)
exactly, alongside running my own discord bot
ok ill try that
If you load your telegram stuff as a cog then it should be pretty easy
the button is not a view
oups mb i did not see it was a button
is there a way to make my mutes basically timed? Like after a certain duration the mute will be revoked
i think of hooking a async method with await asyncio.sleep and then triggering the unmute
but i don't think this is the best approach
Models are classes that are received from Discord and are not meant to be created by the user of the library. Attributes key, url. Methods def is_animated, async read, def replace, async save, def ...
ik that and i use that but i also want to make my bot keep track of time
you can check the member.communication disable until
and yes i can see when the timeout gets removed
why?
just let discord handle it
this should do the job so
meh, i also want this time concept for stuff like bans
o
for bans you need a db, else you will run into problems
got it set up already
but you really should let discord handle the timeouts
there is zero reason to make it more complicated
and i mean you have to pass a timeout duration or datetime anyway
mhm
i don't want staff members to simply remove it tho
like yeh they can execute a command to remove it
then dont give them permissions
task that checks whether a ban is overdue every 5m or so
well i kinda care about the individual seconds
yeh... I have an idea tho
is there a way to manually create a task? And tell pycord to execute it after this time
without needing to do this
@tasks.loop(seconds=5)
async def very_useful_task():
print('doing very useful stuff.')
you shouldn't do that
literally 1 line of overhead lol
this is the easiest way you can do it
do not handle bans only in-memory, if your bot shuts down even once, all is fucked
also how much would it cost if i put seconds=1?
try it and find out
and you will have to use an async db connector else your bot will be blocked constantly
but there's no point in running it every second
when do you ban someone for any amount of seconds
mhm
can't i connect, use the connection and when the bot shuts down break that connection?
if you execute a sync db query your bot will be blocked
and if you run the task every 5s itll be blocked constantly
the connection will of course also stop existing when your bot stops running lol
my brain ain't working after 12 hours
but got what u mean
tbh my idea was to use tasks to delay the task for a certain amount of time and then execute stuff after
yea, thats a no no
hm i could use a mix of asyncio sleep and tasks
and predict if the timing hits a later iteration or not
No
if not then sleep the remaining seconds and then unmute
task is not running
def __init__(self, bot):
self.bot: discord.Bot = bot
self.guild = self.bot.get_guild(id)
self.mute_task.start()
@tasks.loop(seconds=10)
async def mute_task(self):
# ...
how do you know its not running
i try to print but doesn't print stuff
(ik i shouldn't print stuff, but was testing out of curiousity)
no problem with printing
discord_channel = discord_bot.get_channel(DISCORD_CHANNEL)
await discord_channel.send(content = text)
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'send'
is the bot actually in that server & channel?
also you dont have to pass the content as kwarg, its positional
it is, it already sent messages a few days ago for testing purposes
https://docs.pycord.dev/en/stable/api/clients.html#discord.Bot.fetch_channel if get returns none
Bots: Attributes activity, allowed_mentions, application_flags, application_id, auto_sync_commands, cached_messages, cogs, debug_guilds, default_command_contexts, default_command_integration_types,...
Tag not found.
Did you mean...
get_x
.get_x
Any function that starts with get_ in Py-cord is retrieving the related object from your bots cache. If the object is not in the bots cache the get_ method will return None. Because of this behavior you should check if the get_x method is None and if it is use the fetch_x method.
Why Is Using fetch_ Without Using get_ First Bad?
The fetch_ method makes a call to the discord API. This API call is unneeded if you already have the information. It will also make your command take longer because it will have to send and than wait for a response from the discord API. It will also contribute to the discord APIs global rate limit of 50 requests per second.
What Is Cache?
The cache is a temporary storage inside your bot. It holds many objects from members to messages. When you restart your bot the cache will be empty. When the cache is full it will delete older objects to make space for the new objects.
I have a strange problem, namely in the navview the first print self.old_interaction does not work but the second and the second one gives me none and with the first one I get this fehelr
Traceback (most recent call last):
File "C:\Users\jurek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\discord\ui\view.py", line 426, in _scheduled_task
await item.callback(interaction)
File "c:\Users\jurek\Documents\Development\Python\DC Bots\Xenority\Xenority-Main-Pycord-V2-PY\cogs\dev\embed_builder_v3.py", line 45, in callback
await interaction.edit(embed=embed, view=NavigateEmbedsView(embeds=embeds))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\jurek\Documents\Development\Python\DC Bots\Xenority\Xenority-Main-Pycord-V2-PY\cogs\dev\embed_builder_v3.py", line 940, in __init__
self.add_action_view()
File "c:\Users\jurek\Documents\Development\Python\DC Bots\Xenority\Xenority-Main-Pycord-V2-PY\cogs\dev\embed_builder_v3.py", line 955, in add_action_view
print(self.old_interaction)
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NavigateEmbedsView' object has no attribute 'old_interaction'
def __init__(self, embeds, start_index=0, template_name=None, interaction: discord.Interaction = None):
super().__init__()
self.embeds = embeds
self.current_index = start_index if 0 <= start_index < len(embeds) else 0
self.template_name = template_name
self.current_site = self.current_index + 1
self.add_editor_view()
self.update_buttons()
self.add_action_view()
self.old_interaction = interaction
self.add_template_view()
def add_editor_view(self):
self.editor_view = EditorView(self.template_name)
for item in self.editor_view.children:
self.add_item(item)
def update_buttons(self):
self.add_item(PreviousButton(self))
self.add_item(EmbedInfoButton(self))
self.add_item(NextButton(self))
def add_action_view(self):
#print(self.old_interaction)
if self.template_name is None:
self.action_view = ActionEmbedsView("Send", "Save Template", "Share", "Template Name", "Choose a Template name", "Title")
else:
self.action_view = ActionEmbedsView("Send", "Update Template", "Share", "Template Name", "Choose a Template name", "Title", self.template_name)
for item in self.action_view.children:
self.add_item(item)
def add_template_view(self):
print(self.old_interaction)
if not self.template_name is None:
self.template_view = TemplateActionsView(f"Current Template: {self.template_name}", self.template_name,
self.old_interaction)
for item in self.template_view.children:
self.add_item(item)```
well, pass "interaction" then lol
no i need the none print but from the first i become the error
ok
yea because you print it before interaction is assigned
bot on start up is always coming up with this but using a different bot token its fine, the bots have admin and have access to everything in the servers
Ignoring exception in on_connect
Traceback (most recent call last):
File "/Users/ollie/Github/104Systems/.venv/lib/python3.11/site-packages/discord/client.py", line 400, in _run_event
await coro(*args, **kwargs)
File "/Users/ollie/Github/104Systems/.venv/lib/python3.11/site-packages/discord/bot.py", line 1178, in on_connect
await self.sync_commands()
File "/Users/ollie/Github/104Systems/.venv/lib/python3.11/site-packages/discord/bot.py", line 754, in sync_commands
app_cmds = await self.register_commands(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ollie/Github/104Systems/.venv/lib/python3.11/site-packages/discord/bot.py", line 531, in register_commands
prefetched_commands = await self._bot.http.get_guild_commands(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ollie/Github/104Systems/.venv/lib/python3.11/site-packages/discord/http.py", line 367, in request
raise Forbidden(response, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50001): Missing Access
any ideas as to why this happens, it doesnt appear to affect the bot when its running, just annoying to see in the terminal
@tiny sable got the same issue maybe he has a solution
it can be because u have a debug guild set
as soon as i send a msg in here after looking for the better part of an hour, i find the issue ffs
still havent a solution
my workaround is testing on prod xd
BUT a friend told me this is most likely a discord API bug
admin isn't a fix all in bots
give them explicit permissions for the channel
and every permission individually in the roles
If I replace get with fetch channel, I get an error
Traceback (most recent call last):
File "e:...\mainv5.py", line 37, in echo
ds_channel = await discord_bot.fetch_channel(DISCORD_CHANNEL)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:...\venv\Lib\site-packages\discord\client.py", line 1805, in fetch_channel
data = await self.http.get_channel(channel_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:....\venv\Lib\site-packages\discord\http.py", line 285, in request
async with self.__session.request(
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: '_MissingSentinel' object has no attribute 'request'
can you show your bot definition
got it fixed
thx
never seen that before ngl
was it that?
buried in one of the util files was a 1 liner that was looking for a channel that doesnt exist
discord_bot = discord.Bot()
not sure if this is what youre looking for, lemme know if it isnt
and you actually run the bot?
Just show your main file
well in the code I did write it but Im pretty sure its getting blocked but my telegram userbot's app.run()
Ill send the main file
import discord, os, traceback
from dotenv import load_dotenv
from pyrogram import Client, filters
print('Starting...')
OWNER = -1111111111
TELEGRAM_CHANNEL_SOURCE = - -222222222
DISCORD_CHANNEL_RECIPIENT = 3333333333
session_strings = {
"userbot1":"abc",
}
load_dotenv()
discord_bot = discord.Bot() #discord bot login
app = Client('ubot1', session_string=session_strings['userbot1']) #telegram userbot login
@app.on_message(filters.channel)
async def echo(client, post):
print(post)
try:
print(f"{discord_bot.user} is ready and online!")
ds_channel = await discord_bot.fetch_channel(DISCORD_CHANNEL_RECIPIENT)
try:
if post.media.__str__() == "MessageMediaType.VIDEO" or post.media.__str__() == "MessageMediaType.PHOTO" or post.media.__str__() == "MessageMediaType.DOCUMENT":
post_caption_da_mandare = post.caption.markdown
with await post.download(in_memory=True) as post_bytes:
post_bytes.seek(0)
await ds_channel.send(content = post_caption_da_mandare, file=discord.File(post_bytes))
elif post.media.__str__() == "MessageMediaType.WEB_PAGE":
await app.send_message(OWNER, f'Webpage not sent')
await app.forward_messages(OWNER, TELEGRAM_CHANNEL_SOURCE, post.id)
print(post)
elif post.text:
post_text_da_mandare = post.text.markdown
await ds_channel.send(content = post_text_da_mandare)
print(f'text post sent')
else:
print(post)
except Exception as post_exc:
print(f'{traceback.format_exc()}\nCause:')
print(post)
except Exception as e:
print(traceback.format_exc())
app.run()
print('logging in discord') # never gets printed, most likelblocked by app.run()
discord_bot.start(os.getenv('DISCORD_BOT_TOKEN'))
print('logged in discord')
Bot.startin pycord is async- Similarly, in pyrogram
Client.runis also blocking so your bot would never start regardless
pyrogram's run is actually very similar to pycord's run, using the same technique
i would say, do the following:
- override
discord_bot.startto prefill your token:
async def start():
await discord_bot.login(token)
await discord_bot.connect()
discord_bot.start = start```
2. after doing so, try to use `compose` as demonstrated in the pyrogram docs <https://docs.pyrogram.org/api/methods/compose#pyrogram.compose>
i can't guarantee any of that works, you'll need a fair bit of debugging to pull it off
What is the command that sends the comparison of discord.client bot bridge bot etc...
discord.Client # just for events
discord.Bot # events + slash/user/msg commands
commands.Bot # above + prefixed commands
bridge.Bot # above + bridge commands (application commands and text commands in one)
Thanks
Thanks, is this what you intended?
async def discord_start():
await discord_bot.login(os.getenv('DISCORD_BOT_TOKEN'))
await discord_bot.connect()
discord_bot.start = discord_start
asyncio.run(discord_start())
app.run()
I think this would block app.run()
Regarding the last suggestion, I don't think I need the compose method as I'm using only one pyrogram client.
does anyone have any idea why this command is the only (guild) one that pops up in dm?
I thought I forgot to sync it after some changes, but it looks like that's not the problem, especially since some of the others have the same structure 
Is it possible to set a role as default in a role select?
I think (?) guild_only is deprecated in 2.6
Its because it is a public command
what
i got no warnings with decorator but yeah ive seen
what
re
so no depre for this
as it sets contexts
only for
@commands.slash_command(guild_only=...)
anyway its kinda strage lol
hmm let me check something
intresting
this is the ony case when its false lmao
nvm im blind
it should be discord.guild_only
hahahah
I don't know why, I totally didn't notice it and in the conversation above I was checking the right decorator
thanks toothy I think that if I hadn't started digging deeper I wouldn't have even noticed it at the current moment
wait what was the solution now
i used commands.guilld_only instead of discord.guilld_only
my point is that, at a glance, compose also works with the discord bot (after overriding start as i suggested)
can you edit files when editing a message?
i wanna sync back the components when the bot goes inactive and active again
and well my message is an embed
which has a thumbnail as a local file
previously i was using discord CDNs but better migrated towards local files instead
file=
.local
.tags local
.local-file
f = discord.File("some_file_path", filename="image.png")
e = discord.Embed()
e.set_image(url="attachment://image.png")
await messagable.send(file=f, embed=e)```
oh wait nvm,
Hello! How do I properly allow a certain role to view a voice channel? I used view_channel=True, but the position is set to neutral (/)...
show your code
Is there a way to know if you're running in a user install context or a guild install context outside of checking if ctx.guild.me is None?
ah, ctx.interaction.context
No, that's not the one
Uhh it's something called app_context
.rtfm app_context
Target not found, try again and make sure to check your spelling.
You were actually right. It is interaction.context
Models are classes that are received from Discord and are not meant to be created by the user of the library. Attributes key, url. Methods def is_animated, async read, def replace, async save, def ...
that tells me what location context its in (i.e. DM/Guild/whatever else). I need to know whether im running in a context where the bot is in the server or not, because half of the Guild data is just None in that case
This is incredibly poorly named lmao
Ah
You have to check if ctx.guild is None
That's the way to check if you're in a guild or not
how do i make it so that a discord.Option can have a discord attachment
set discord.Attachment as the input type
i did, didn't work
discord.Option(name="file", description="Upload your .replay file", required=True, type=discord.Attachment)```
Yeah but ctx.guild itself isn't None in these instances - is there no proper way to check outside of doing if ctx.guild and ctx.guild.me?
nothing wrong with that, it just feels inefficient lol
How it isn't None?
If you're outside a guild, aka DMs, guild will be None
Type is positional. discord.Option(discord.Attachment, ...
No I'm talking about actual installation contexts
required is True by default, you can remove that
these things
user install is where commands are available everywhere without the bot being in the guild
Then we go back to the first thing
Models are classes that are received from Discord and are not meant to be created by the user of the library. Attributes key, url. Methods def is_animated, async read, def replace, async save, def ...
So is it guild regardless of whether the bot is installed in the guild?
I swear, app commands get more ambigious with each api update
nothing was wrong with regular text commands
I think you could check the authorising owners https://docs.pycord.dev/en/master/api/models.html#discord.Interaction.authorizing_integration_owners
Models are classes that are received from Discord and are not meant to be created by the user of the library. Attributes key, url. Methods def is_animated, async read, def replace, async save, def ...
If you get a user_id, then you can assume the command is ran in a user installed context
I haven't played around with installable apps yet
Ah that looks like what im looking for
ambiguous how?
everything was wrong with them, literally everything
well on topic, there's ambiguity around "contexts" - install contexts and "command invocation" contexts (e.g. DM/Guild/Private)? and then there's the library specific ApplicationContext, BridgeContext and commands.Context. There's also "command contexts" in the sense of slash command, message command, and user command. "user context" could either refer to the install context, or the right-click context
I'll rephrase then, they were "simpler"
you got a message, in a channel, sometimes in a guild, from member, or user otherwise
I might just prefer the platform from a time long ago, but I really do think that app commands have just added more hurdles to the process
credit where its due, they have added some really nice things, I just think they're more of a hassle
not confusing to me tbh
there is no such thing as user context apart from the installation context. The ctx for slash and context menu commands is ALWAYS applicationcontext because it's application commands
that's what I mean by non-installation user context
I could not be paid to make message commands anymore
it's too annoying
that's a user command
no
man ive been convinced since their initial release they were "user context" ๐ญ
context menu commands as a whole, for both message and user commands
ah that'll be why I though that
Why ? Canโt you just add both decorator ?
discord.ext.bridge would be better suited than using both decorators ^^
Ooo he means prefix command
I was thinking he was meaning message command like user command
I tend to prefer prefix as slash command, but I implemented both creating my ยซbridgeยป thing because itโs easier for the new user of the bot
any tips on optimising my bot for modlogging and stuff?
it takes ~3 seconds to do either unmuting or muting
well, ever measured what takes so long?
because a normal discord timeout call sure doesnt take 3 seconds
this isn't the only thing im doing
yea then measure what takes so long
indeed, logging is typically <1s
you receive events within a couple ms (latency dependent), anything beyond that is your own overhead
You can gather multiple asynchronous tasks too run them concurrently of that can help you
it didn't do much
im interacting a lot with the discord API
i add a role, create a DM channel, send a embed in the DM channel, send an embed in the current channel
man i wish you could group these actions into 1 HTTP request (that depends on discord themselves)
like you could do
await discord.execute_multi_action(
ctx.followup.send(embed=embed, file=thumbnail, ephemeral=True),
target.timeout(delta, reason=reason),
target.add_roles(mute_role, reason=reason),
target.create_dm()
# ....
)
why followup.send, just use .respond
i defered the response
and no, you cant make multiple API requests in one, that makes no sense
yea, and you use .respond to respond.
whats the difference using .respond and not followup
its a shortcut really
respond is how you should do it
it handles it appropriately, regardless of whether its been deferred or not, or even if you already responded
oh ok
also uh
im not sure what the plan here is
oh wait, no, i get it
but why lol
there's no reason to wrap it in a function
well this was just an example
ah
but the problem rn is first it takes more time for discord to handle like 5 different requests than it is for 1
i mean yea, but, you cant combine API requests
it makes no sense, thats not how APIs work
and i doubt thats why it takes so long, did you measure the time?
yea so what takes long
the requests, sending messages.... etc.
also, don't do member.create_dm
like this?
target.can_send()
but really, just put it in a try except
yea
but as i said it has some drawbacks and im not sure if it can detect whether you can DM the member at all, test it
ok
oh yea i think you also need to put text in there
i have the feeling it improved the performance and shaved off a second
like this?
embed, thumbnail, view = await other_embeds.generate_dm_mute_embed(msg, bot, target, reason, duration_secs)
can_send_to_target = target.can_send(embed=embed, file=thumbnail, view=view)
if not can_send_to_target:
return
dm_msg = await target.send(embed=embed, file=thumbnail, view=view)
yea
try it with an account you disable DMs on
mhm
i get
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: Forbidden: 403 Forbidden (error code: 50007): Cannot send messages to this user
but
can_send_to_target = False
try:
can_send_to_target = target.can_send()
except discord.errors.Forbidden:
return
if not can_send_to_target:
return
oh
xD oh sorry
it actually did improve performance
removing the create dm channel
Yeah, srry for late reply
clan_role = discord.utils.get(clan_guild.roles, name=clan_name)
new_text_channel = await clan_guild.create_text_channel(name=text_name, category=clans_category, position=2)
new_voice_channel = await clan_guild.create_voice_channel(name=voice_name, category=clans_category, position=1)
await new_text_channel.set_permissions(clan_role, view_channel=True)
await new_text_channel.set_permissions(clan_guild.default_role, view_channel=False)
await new_voice_channel.set_permissions(clan_role, view_channel=True)
await new_voice_channel.set_permissions(clan_role, connect=True)
await new_voice_channel.set_permissions(clan_guild.default_role, view_channel=False)
Some classes are just there to be data containers, this lists them. Unlike models you are allowed to create most of these yourself, even if they can also be used to hold attributes. Nearly all clas...
id do it with permissionoverwrite
yes you can do kwarg permissions but meh
also remember that the bot has enough permissions
how does role comparison work?
I'm creating a discord.Object(role_id)
can i == this to the actual role itself and get true?
basically
discord.Object(role.id) == role
ctx.defer() can it be ephemeral?
Nope, because eq also checks class
Yes
thank you
damn
asyncio.all_tasks() -> check type
Thanks I will try to check ca tonight, I guess I can then start and stop them using the loop star and cancel or should I do another thing
(it makes slightly more sense if you print it and see what you're dealing with)
I mean, I'd personally have all tasks in a single cog so it's easier to manage
Itโs more to do check if a loop has stopped while I shouldnโt
Like a discord error connection that cancel an essential loop
And restart them in case
is it possible to make the context menu options have a value on autocomplete?
so visually it is A B C but when i get the value its abc
i got this
tag: discord.Option(
str,
description="The faq tag to choose from",
autocomplete=discord.utils.basic_autocomplete(get_tags)
)
oh wait
i did this
def get_tags():
faq_document_path = Path("./assets/documents/faq")
faq_documents = faq_document_path.glob("**/*.json")
l = []
for doc in faq_documents:
with open(doc, "r") as f:
data = json.load(f)
l.append(discord.OptionChoice(name=data["tag"], value=str(doc)))
return l
tag_choices = []
class FaqCog(commands.Cog):
def __init__(self, bot):
self.bot: discord.Bot = bot
@commands.slash_command()
async def faq(
self,
ctx: discord.ApplicationContext,
tag: discord.Option(
str,
description="The faq tag to choose from",
choices=tag_choices
)
):
# ...
def setup(bot):
global tag_choices
tag_choices = get_tags()
bot.add_cog(FaqCog(bot))
i could put them manually ik but i prefer automating that
Isnt opening a json blocking?
Ignoring exception in on_connect
Traceback (most recent call last):
File "F:ยฅProjectsยฅAkabotยฅ.venvยฅLibยฅsite-packagesยฅdiscordยฅclient.py", line 400, in _run_event
await coro(*args, **kwargs)
File "F:ยฅProjectsยฅAkabotยฅ.venvยฅLibยฅsite-packagesยฅdiscordยฅbot.py", line 1178, in on_connect
await self.sync_commands()
File "F:ยฅProjectsยฅAkabotยฅ.venvยฅLibยฅsite-packagesยฅdiscordยฅbot.py", line 735, in sync_commands
registered_commands = await self.register_commands(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:ยฅProjectsยฅAkabotยฅ.venvยฅLibยฅsite-packagesยฅdiscordยฅbot.py", line 599, in register_commands
registered = await register("bulk", data, _log=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:ยฅProjectsยฅAkabotยฅ.venvยฅLibยฅsite-packagesยฅdiscordยฅhttp.py", line 373, in request
raise HTTPException(response, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In 32.options.0.choices.0.value: Could not interpret "['ฤeลกtina - 98%', 'English', 'Franรงais - 97%', 'ๆฅๆฌ่ช - 95%']" as string.
I need help, this error wasn't happening yesterday
yea, but its only opened once
then undo what you did since it last worked
Choices got a limit of 25
that's not the issue
There's 4 items in the choice, and I'm only going to be translating to ~10 languages so I can rule that out
it doesn't work for me
like i tried it before doing this
it's literally what you're doing with extra steps.
oh
call function that returns list > save to variable > pass variable with list in it to choices kwarg
or
call function directly and pass the list directly into the choices kwarg
also the thumbnail
if data.get("thumbnail") is not None:
f = discord.File(fp=Path("./assets/images/" + data["thumbnail"]), filename="faq_thumbnail")
embed.set_thumbnail(url="attachment://faq_thumbnail")
doesn't work
for some reason
even tho i send the file
do you actually attach it to the message?
yes
thumbnail = None
with open(tag, "r") as f:
data = json.load(f)
embed = discord.Embed(
title=data["title"],
description=data["description"],
color=discord.Color.blurple()
)
if data.get("thumbnail") is not None:
thumbnail = discord.File(fp=Path("./assets/images/" + data["thumbnail"]), filename="faq_thumbnail")
embed.set_thumbnail(url="attachment://faq_thumbnail")
await ctx.response.send_message(embed=embed, file=thumbnail)
Does the choices list support Asian languages? Because ๆฅๆฌ่ช is Japanese
supports any string for all I know
you can use ctx.respond here too btw
oh yeh srry
can you show what it looks like in discord
the entire message
this list is valid for choices=?
should be
tried the same but without the Japanese (?) characters?
hm
can you try just attaching the image to the message to see if the image works in the first place
ah
local files are a lil clunky to work with, should be abstracted away
tried this
same thing
why is it interpreting as a string
I put in something into the list
but still
can you show how you define the option
I didn't change this line of code
I've changed the get_language_names method
to return a hardcoded list
even this doesn't work
putting it in a list and using * to unpack list
which in python console returned the exact same thing as calling it directly
ok so now I'm confused, this doesn't work either
and if you just put choices=["string"]
ah
this shouldn't work but can you add str as positional argument to the option
this?
no, just put str, at the beginning of your option decorator
like this?
yea
I've tried type=str too
I really don't know why it does that then
that didn't work either
input_type I think
name is fine but just remove the str at the option()
look at the actual issue tho
out of all of this it's the only one that doesn't work
option(name="")
it's the one at per_user_settings:31
all of these are defined, name=<name>, description=<description>, choices=[<choices>]
this one doesn't have issue and it includes East Asian characters
legit no idea
seems like a really weird bug
maybe remove the choices entirely, restart, and then add them back
lol what
and even if I remove the file completely it throws the error
show
idk what's happening
OH
@bot.command()
async def create_role(ctx, name:str, colour):
guild = ctx.guild
member = ctx.author
role = await guild.create_role(name=name, colour=colour)
await member.add_roles(role)
await ctx.send(f'Role "{name}" created successfully.')
why 
Add "encoding="UTF-8" to the file
ah the classic
first of all, use ctx.respond
what in the world is that
i dont use application commands
prefix??
pls 
ok cursed
staying classic
nvm then
i always loved the "pls " prefix
uhh yea you did your f string wrong
somehow
only reason id use application commands is to use them everywhere
how do you know its app commands
I'm going to check usages of the get_language_names method
we are opposites
me?
which Line throws this error
lol
thats a good question!
core.py line 244.

you know its a bad problem when even the server's helper doesnt know what the fuck is wrong with your code
im just some guy
youre cool in my eyes
so like colour:int
its read as string currently
im not quite sure how you'd take a hex color and convert it into a proper format rn
but it should work if you cast it as int
and then put like 0x34213 or whatever
oh is the colour im using uhhh
messed up
show what you put in chat
idk what kind of character that is, but it has to be an x
that looks like some mathematical x lol
time to try that out
same error
whats a typehint if you dont mind me asking
: str
as long as you type it the same
now theres this
this is even worse
im not sure i did it right
quick check
@bot.command()
async def create_role(ctx, name:str, colour):
guild = ctx.guild
member = ctx.author
role = await guild.create_role(name=name, colour=int(colour))
await member.add_roles(role)
await ctx.send(f'Role "{name}" created successfully.')
im just taking this from the python docs rn lol
try int(colour, 0)
if that doesnt work then i dont know
nice
embed.set_image()
can i set a video with that?
image and gif
is it normal that discord.Member allow user ?
like how?
if you mean slash options, yes
in the slash
why is it intentional ?
but validating it isn't particularly hard
Hey all is this the right way to have an optional arg? its still saying its required when i try to test it
@discord.option("pair", type=str)
@discord.option("start_date", type=str)
@discord.option("end_date", type=str)
@discord.option("fees", default=0.001, required=False, type=float, min_value=0)
@bot.slash_command(description="Optimize a strategy")
async def optimize(ctx, pair, start_date, end_date, fees):
options goes after the bot.slashcomร d
you can pass the type positionally as well, no need for type=
yep that did it thanks!
not sure what you mean here?
just remove type=
and move the input type to the second position for your last option
the order of the positional args is name, type so there's no need to pass it as kwarg
so this?
@bot.slash_command(description="Optimize a strategy")
@discord.option("pair", type=str)
@discord.option("start_date", type=str)
@discord.option("end_date", type=str)
@discord.option("fees", type=float, default=0.001, required=False, min_value=0)
async def optimize(ctx, pair, start_date, end_date, fees):
remove type=
ahh ok
and technically you dont even need it for str, because options are strings by default
also id highly recommend typehinting the parameters in your function definition, especially ctx
helps a lot during development
i need to type hint as well as define the type in the option? i thought doing type= was equivalent
what would the ctx type hint be?
because else your IDE wont suggest anything if you do parameter.<anything>
discord.ApplicationContext
Hello, is it possible to create a private user bot (that only the owner can add) ?
just set it to private on the developer portal
this toggle also takes effect on user installations
thanks, i forgot to disable the bot option on the link generator, that's why it wasn't working
fixed now
What if I want my slash commands available only in guilds and not in private chat
assuming you are using @bot.slash_command()
@bot.slash_command(..., contexts={discord.InteractionContextType.guild})
Thank you
Another one
Command.call() missing 1 required positional argument: 'context'
@bot.hybrid_command()
async def purge(ctx:commands.Context, limit):
await purge(limit=limit, bulk=True)
await ctx.send(f'Successfully purged {limit} messages.')
no, thats called bridge

Concept
so do i use that instead
also please just look at the first line of this function
there is something very wrong
not "instead", hybrid_command just isnt a thing, it cant work
it does work though so ill keep it
then you aren't using pycord
hold on
show your pip list
i think you are using discord.py
because i think they call it hybrid command
im pretty sure i have that
yea, then you are wrong here lol
no
oh fuck me i used the discordpy doc for my whole code
lol
thats a user app
if you wanna keep using discord.py you'll have to ask them how to do it
too much work
?
alright whats the discordpy server
google it
or its on their website
oh yup found it
I have a very weird problem:
If a channel is moved to another category, the new channel is not part of the .channels list of the category object (but in discord I can see it is). I have to restart the bot, then its there. The weird thing is, the same code worked without any problems like a year long and since a few days I have this problem. Some change in the API?
category = discord.utils.get(bot.guilds[0].categories, id=id_int)
all_channels = category.channels
still havent fixed this though
Is it possible to check if the current message is being sent as ephemeral? while I do know ctx.response.defer(ephemeral=True), in my case its user app slash command, which it automatically sets the message ephemeral if it detects Use External Apps server permission is denied. The command has ctx.send commands which it will cause MissingAccess error if it tries to send message if the command is user-installed
ctx.authorizing_integration_owners i think
that will tell you if its installed as user app or on the guild (or even both)
or lock the entire command to only being used when the bot is guild installed
eh
'ApplicationContext' object has no attribute 'authorizing_integration_owners'
but this is exactly what i need
mb its in interaction
ctx.interaction.authorizing...
https://docs.pycord.dev/en/stable/api/models.html#discord.Interaction.authorizing_integration_owners
Models are classes that are received from Discord and are not meant to be created by the user of the library. Attributes key, url. Methods def is_animated, async read, def replace, async save, def ...
lil bit of everything
The link seems to be broken / ur WiFi
honestly i have no idea where discordpy and pycord is used in my code ๐ญ
then decide for one and make it equal across your bot
neither
their code seems to use discord.py sooo 
but the question is: which one is better
for anything but prefix commands pycord
hands down
and in prefix commands they're largely the same under the hood
ok then ill use pycord
Why prefix command is under the hood ?
ill just delete discordpy i guess
pycord forked from discord.py when application commands were just announced
thus the code for all the prefix stuff is still the same for a lot of it
unlike for application commands where discord.py is completely shit
and pycord has its own implementation
discord.ext is discordpy right
pycord has that path too
uhhhh.........
just uninstall anything called discord (and pycord if you have it), and then ONLY do pip install py-cord
oh well
okay
done
pretty sure
show your pip list
and also restart your IDE


