Resetting a nickname - discord.py

Is it possible to reset a nickname of the target member. The case with the shown below is that when a member first joins he gets [] before his name. Then we use this to add something in the []. But when you want to replace the value inside the [] you get it like [] <old.value> nickname. Thus i need to reset it first when the command is used on members that already have some name in those [].
#command(name="nickname")
async def nick_change(self, ctx, member: Member, nick) -> None:
old_nick = member.nick.replace("[", "").replace("]", "")
await member.edit(nick=f"[{nick}] {old_nick}")
await ctx.send(f"{old_nick}'s corp set to {nick}")

Related

Multiple SlashOptions Nextcord

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)

Discord.py Send a message after user reacts to a message

I'm trying to make my discord bot send a message as soon as a reaction has been added to a specific message.
This is my code:
#client.event
async def on_raw_reaction_add(message, reaction):
message_id = message.message_id
channel_id = 830438815595364372
if message_id == 830439131083046952:
if reaction.emoji == 'πŸ‘':
await channel.send('OK')
I'm not sure what I'm doing wrong, when I react to the message, nothing happens.
Thanks in advance.
L.
Your code seems a bit messed up to me. You have to use payload to get it working.
Have a look at the following code:
#client.event
async def on_raw_reaction_add(payload):
# channel and message IDs should be integer:
if payload.channel_id == ChannelID and payload.message_id == MessageID:
if str(payload.emoji) == "YourReaction": # Use a string
channel1 = client.get_channel(MessageChannel)
await channel1.send("Test")
What did we do?
Used payload
Defined the channel and message ID
Defined the channel where to send the message after the reaction
Note that the on_raw_reaction_add() event reference takes only one argument, payload. The payload argument is a RawReactionActionEvent object, which has a few key attributes for solving this issue: message_id and emoji.
The message_id attribute of the RawReactionActionEvent object can be compared to the target Message ID. RawReactionActionEvent.emoji is a PartialEmoji object, which has an attribute name, which returns the name of a custom emoji or the unicode representation of a default emoji. This can be compared to the target emoji, whether if it's the emoji copy-pasted or the unicode representation of the emoji.
Finally, Client objects have a method fetch_channel which takes the target Channel ID as its only argument and returns a GuildChannel object, which has the send() method which we all know and love.
#client.event
async def on_raw_reaction_add(payload):
message_id = 830439131083046952
channel_id = 830438815595364372
if payload.message_id == message_id and payload.emoji.name == "πŸ‘": # OR paylod.emoji.name == u"\U0001F44D"
channel = await client.fetch_channel(channel_id)
await channel.send("OK")

Checks For Reactions Discord.py

So I want to make a text game that edits the character every time the author who called the command reacts. My code so far for adding reactions:
#client.command()
async def test(ctx):
msg = await ctx.send('Hi')
up = '⬆'
down = '⬇'
left = 'β¬…'
right = '➑'
await msg.add_reaction(up)
await msg.add_reaction(down)
await msg.add_reaction(left)
await msg.add_reaction(right)
This adds the up arrow, down arrow, left arrow, and right arrow to the message "Hi". I want to see if someone clicked on the arrow and if that someone is the author of the command. I have no idea how to get if the author of the command clicked on the arrow reaction. Any help would be appreciated.
If you're waiting for a reaction, use wait_for() with the reaction_add event as the positional argument.
To limit it to the invoker, you can create a check and pass it into the check kwarg of wait_for(). The check would take in two arguments and you only need to compare if ctx.author is the same as the check author.
There is an example for wait_for() in the documentation
References: https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=wait_for#discord.ext.commands.Bot.wait_for

how to check if a channel name starts with a certain string discord.py

#client.command()
async def delete(ctx):
channel = discord.utils.get(ctx.channel.startswith("Ticket"))
await ctx.channel.delete()
as stated in the title, how do i check if a channel starts with the string "Ticket", not if its named ticket, im trying to make a ticket system, thanks.
You can loop through all text_channels and then compare the TextChannel.name with what you want.
It will be a sting so you can use startswith or =='Name' if you want an exact match.
It is better to send a message there in testing instead of deleting it.
#client.command()
async def delete(ctx):
for channel in ctx.guild.text_channels:
if channel.name.startswith('Ticket'):
await channel.send('Deleting.....')
#await channel.delete()

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)

Resources