what does the "ctx" really doing discord.py - python

Im programming a discord bot in Python
and every tutorial on Youtube telling me that i have to use the 'ctx' when im making a command,
but none of those Youtubers explaining what this is doing, so pls can someone answer me?

As explained in the docs:
A command must always have at least one parameter, ctx, which is the Context as the first one.
Now, what is Context? Again, the docs:
Represents the context in which a command is being invoked under.
This class contains a lot of meta data to help you understand more about the invocation context. This class is not created manually and is instead passed around to commands as the first parameter.

Related

Discord.py mention application command

I am trying to make a help menu for my discord bot, and would like to make it similar to MEE6's, where it mentions application commands.
But rather than typing myself each command by hand, I would like to automate the process, so that if I add a new command it automatically adds it to the list.
I can easily obtain the commands with tree.get_commands(), but I don't know how to mention them: I found that typing "</command_name:command_id>" works, but I find nowhere on the docs how to get the id of an application command. Currently the only way I know of is with Discord Developer Mode.
Is there something I'm missing?
For anyone who ecounters this problem: I was not using the correct command.
I wanted to get type discord.AppCommand, but tree.get_commands() returns discord.Command.
So you get all the Application Commands using await tree.fetch_commands(), and those actually have the id that can be used to mention the command.

How to make my Discord bot respond to both custom prefixes and mentions when I use a function to get the prefix?

I would like to preface this with saying that I asked this question previously and it got closed, marked as a duplicate. I saw those other questions before. I tried those solutions. They didn't work. They don't work with prefix functions, only prefix strings. This is why I asked the question. I have reworded a few things, added info to the title, as much as I can think of to get this question through so I can get the answer I need. Please, please do not close this one. I want an answer that works.
I have a function that retrieves a custom prefix from a database file, but I would like to implement the ability to also respond to mentions, as in the when_mentioned() function. However, I can not pass in both functions to the command_prefix parameter when I am initializing my bot, nor can I add my prefix function to the when_mentioned_or() function to combine the two. Is there any way I could go about doing this? I've been searching for answers for the last hour and have found nothing that works yet.
You can simply use the when_mentioned function in your own function that retrieves a custom prefix. You can call it with the same bot and msg that you're passed and it will return a list of the mention formats (for with and without a nickname) that you can use. You can then return that list with your custom prefix appended.
Alternatively, you can simply directly return a list with your custom prefix, bot.user.mention (see the documentation for Bot.user and ClientUser.mention), and the nickname format for that mention (<#!{user_ID}> rather than <#{user_ID}>; see the documentation for ClientUser.id).

discord.py - Obtaining channel object from mentioned channel

I am looking for a way of which I can obtain the name and ID of the channel, from when someone sends a command followed by a channel mention, such as --command #CHANNEL. I have seen this done on other public bots, but I have, as of yet, found no way to reproduce this. This is probably a stupid question, but I would highly appreciate any help or thoughts you could put towards this.
Thanks in advance,
Nat.
You would need to make sure a channel is an expected parameter in the command definition. This code creates a command for the bot called 'test' that expects an object of the discord.Channel class to be passed as a parameter:
bot.command()
async def test(channel: discord.Channel):
You could then get the name and id using channel.name and channel.id, respectively.

Python, Returning object class names by a function

I'm new to Python and this is my first question here. Hope any of you guys will be able to help me out.
I'm trying to call values inside an object from an external program. The object that I'm trying to access is given in a class (as i uderstand it), and the name of the class may change according to X, see below:
External programs object and class information
I want to be able to call information from Phase_6 in this case, however it could be Phase_12 in another case. I was considering making a function where i could have the _'Number' as an input. But I can't seem to find any information of how to do such.
I was thinking of something like using +str(X), as I do when plotting. But as it is probably not a string, it doesn't work out.
My proposed code
Ive read that bpy in Blender may be able to replace the name of the class that i want to return, however I'm not sure if it'll work, and I dont want to switch editor :)
Hope you guys can help me out,
Joachim
Found the answer, one could use getattr.
x = 6
result = getattr(g_o, 'phase_'+str(x)).Info.SumMsf.value
Thanks anyway - And I'll work on the pictures
Joachim

Get functions docstring/inital comment [Python]

So I recently ran into a framework (for a chat bot) that was very clever.
It used a combination of things to make it extremely dynamic. Most of these things I already know how to replicate. Yet there was something that really caught my eye, and made me really wonder.
How do you obtain the comment?
def foo():
'''My function comment'''
return 'foo!'
In this framework: It would pull the comment, and use it as it's help.
So for example we say !help foo
It would return My function comment
It really boggles my mind, since I always thought comments weren't kept in memory. So I have to assume it's using some kind of inspection on it's own file. I'm just very curious on how this works, and if anyone has any libraries that would help with this, please let me know.
Edit: for anyone wanting to look at the framework; here is the link
That's not a comment - comments are lines that start with #
This is a docstring.
You can access the docstring using foo.__doc__
The help function would thus be
def help(thing)
return thing.__doc__
When you type !help foo behind the scenes the framework calls help(foo).
You can access it with .__doc__

Categories