Multiple SlashOptions Nextcord - discord.py

I am trying to to create a command that that allows a user to select multiple options and then put them in an embed. So far I have been able to do this with one option but I want to do it with more as well. Is there a way to add multiple SlashOptions to a slash command and if so how might it be done. Thank you for all your help.
Here is what I currently have
async def announce(interaction: Interaction, buysell:str = SlashOption(
name="bto-stc",
choices={"BTO": "BTO", "STC": "STC"}),
stock:str,):
embed = nextcord.Embed(description=f"**Options Entry**", color=0x00FF00)
embed.add_field(name=f"** [🎟️] Contract:** *{buysell}* *{stock}*", value="⠀", inline=False)
await interaction.response.send_message(embed=embed, ephemeral=False)```
Here is what I would like the end product to look like.
https://i.stack.imgur.com/9SmIK.png

Your stock argument must be before your choices argument as for Python this argument is and optional argument and you can't have required arguments before optionals ones.
Also if you don't want your buysell argument to be optional you need to add a required argument set to True in your SlashOption object. And in your case you can also replace the choices argument in the SlashOptionobject by a list as the choices and the data you want from them are the same.
Example:
async def announce(interaction: Interaction, stock:str, buysell:str = SlashOption(
name="bto-stc",
choices=["BTO", "STC"],
required=True)):
embed = nextcord.Embed(description=f"**Options Entry**", color=0x00FF00)
embed.add_field(name=f"** [🎟️] Contract:** *{buysell}* *{stock}*", value="⠀", inline=False)
await interaction.response.send_message(embed=embed, ephemeral=False)

Related

How to make parameters flexible in ruby?

Im pretty new to ruby . I have a method lets say -" method_X " with parameters (client_name, client_dob).
def method_X client_name, client_dob
(BODY OF THE METHOD)
end
Now I want to introduce a third parameter let's say "client_age". I want my method_X to have a flexibility in taking the parameters.I'm getting an error to mandatorily enter client_name if I forget. I should have flexibility to not mandatorily enter all the three parameters as input. How can I achieve this? Thank you in advance!
In Ruby, you can declare parameters as required, default and optional arguments.
required - required parameters need to be passed otherwise it throws an error.
Ex: def method_X(client_name)
In this, you need to send the client_name argument, else it throws an error.
default - default parameters are optional arguments, but you should declare the default value for the given parameter while defining the method. So that you can skip the argument if you want or you can send a new value while calling the method.
Ex: def method_X(client_name="Abc Company")
In this case, if you haven't passed the client_name argument for the method, the default will be Abc Company. You can default to any value you like, say nil, empty string, array etc.
optional - Optional parameters where you need to use the splat(*) operator to declare it. This operator converts any number of arguments into an array, thus you can use it if you don't know how many arguments you will pass. If no arguments, it gives an empty array.
Ex: def method_X(*client_name)

trying to make a choose command (like pls choose)

I've been trying to code a choose command like how dank memer has one but without the slash commands but the problem is i dont have that much coding knowledge to make the bot choose beetween 2 things so i did a code like this:
#bot.command(pass_context=True)
async def choose(ctx, *, message1, message2):
possible_responses = [
f"i choose {message1}",
f"i choose {message2}",
]
await ctx.send(random.choice(possible_responses) + " ")
the code worked but it only send "i choose" so how can i make my bot choose beetween 2 words and send the answer?
This is a very simple way of doing it,
#bot.command()
async def choose(ctx, option1, option2, option3):
choices = [option1, option2, option3]
await ctx.send(f"I choose, {random.choice(choices)}")

Replying to a message with a embed (Discord.py)

i'm having a problem where i can't reply to a message with an embed.
is this even possible? I'm guessing it is and im just doing it horribly wrong.
My error - TypeError: object method can't be used in 'await' expression
if message.content.startswith('!test'):
await message.reply
embedVar = discord.Embed(description=".order", color=0x7289da)
embedVar.add_field(name='test', value='test')
await message.channel.send(embed=embedVar)
Looking at the discord.py API Reference, it should be possible. Note that Message.reply() is a method, not an attribute. The documentation describes the method:
A shortcut method to abc.Messageable.send() to reply to the Message.
The method takes positional argument content and keywords arguments kwargs, which correspond to the keyword arguments of the TextChannel.send() method. Since embed is one of the keyword arguments, it is one of reply() as well.
if message.content.startswith('!test'):
embed = discord.Embed(description=".order", color=0x7289da)
embed.add_field(name="test", value="test")
await message.reply(embed)
On a side note, I suggest using the discord.ext.commands framework instead of using on_message() and string methods.
You are not using the reply function correctly. You cannot have it by itself, as you have done with await message.reply in your code.
# on_message event, the way you have done it
if message.content.startswith('!test'):
await message.reply("This is a test!")
# or if you prefer the commands extension (recommended)
#bot.command()
async def test(ctx):
await ctx.reply("This is a test!")
# you can also make it so it doesn't mention the author
await ctx.reply("This is a test!", mention_author=False)
# Note that both of the code in the commands example can be used in your on_message event
# as long as you remember to change ctx to message
The above code has been tested as seen in the image below.
As for replying with embeds, I have found that if the bot is only replying with an embed, it may not mention the author.
embed = discord.Embed(description="This is a test", color=0x123456)
await message.reply(embed=embed)
The above code works as seen in the image below.
Try this :
embed=discord.Embed(title="Tile",
description="Desc", color=0x00ff00)
embed.add_field(name="Fiel1", value="hi", inline=False)
embed.add_field(name="Field2", value="hi2", inline=False)
await self.bot.say(embed=embed)
`

Discord python how can I get all the banned users?

I was looking at the API Reference and I found fetch_ban(user). How can I check if the user is banned from the server, I was reading that it returns the the BanEntry, and get a boolean? Can I use member as well or I need to get the user?
Thank you for any reply.
Tip: Always link what you're talking about.
fetch_ban
BanEntry (discord.py source code)
If you go through the source code you will very quickly find this in the first lines:
BanEntry = namedtuple('BanEntry', 'reason user')
Returned is a BanEntry object if the user is banned, otherwise it returns a NotFound Exception.
So to check if a user is banned just do:
async def is_banned(guild, user):
try:
entry = await guild.fetch_ban(user)
except discord.NotFound:
return False
return True
This will also work with members, as they are basically user objects with a bit of extra.
BanEntry is a named tuple (if you need a refresher on those here).
if you wanna command that sending list of banned users
async def banlist(self, ctx):
bans = await ctx.guild.bans()
loop = [f"{u[1]} ({u[1].id})" for u in bans]
_list = "\r\n".join([f"[{str(num).zfill(2)}] {data}" for num, data in enumerate(loop, start=1)])
await ctx.send(f"```ini\n{_list}```")
it gives list like this
[01] 尸仁长仈乃冂仨#0529 (269800030300033098)
[02] Yako#1001 (294113773333557952)
[03] Ping#9216 (46804048093530418)
[04] Vasky#6978 (494069478291921344)

how to store the user input in a parameter in dialogflow

I have a Intent where I have a parameter called age and its value I want it to be the user input.
And in the response I would like to responde with the same input as user given ex:
User Input: Hello, haw are you.
Bot: You said: Hello, haw are you.
So I would need the user input first to store in a parameter and from the Text Response section I can call the parameter but I just don't know how to catch the user input at this moment!
So Text response would be like: You said: $input.
Assuming that you are using Javascript, you can find many useful details here.
Depending on the way you get the input, the solution is totally different.
For instance, in this link you get the input via a click (this.button.addEventListener("click", this.handleClick.bind(this))) or in the following code you get the input as a query.
import {ApiAiClient} from "api-ai-javascript";
import {IRequestOptions, IServerResponse, ApiAiConstants} from "api-ai-javascript/ApiAiClient"
const client = new ApiAiClient({accessToken: 'YOUR_ACCESS_TOKEN'})
queryInput.addEventListener("keydown", queryInputKeyDown);
.textRequest('Hello!')
.then((response) => {console.log(queryInput.value);})
Thus, in the above example, you can use:
value = queryInput.value
you can try it in Inline Editor Fulfillment
function userInput(agent) {
let user_input = agent.query;
//test it
agent.add(user_input);
}

Resources