discord bot - send multiple variables from user - discord.js

I am trying to let user send multiple variables to the bot. types are string, uint.
So in the end, user needs to send 'hello world` and 5.
Is the only way that user sends one string and i should be doing the split in the bot code ? such as user sends hello world&5 and then in bot code, I do msg.content.split('&').
Am I on the right path or is there a better way ?

This is another way to do it:
#bot.command()
async def send_message(ctx, *args):
print(args)
Then users will be able to write for example "hello world" "test 123" test 5 and discord.py will split the input.

Assuming you use JavaScript and discord.js; yes, you are on the right path. Next, you just need to trim the string if users pass spaces around the &.

Related

Make Discord DM a copy of messages

I Have a discord bot that is basically deleting posts that don't match a regex format.
It then DM's the user a quick message that includes the required format.
I am wondering if it'd be possible for the bot to send the user their original message as a second DM or part of the DM it is already sending out.
Here is the code
if valid_check:
if re.match(self.regex, message.content) or re.match(self.regex2, message.content) and not gutted:
pass
else:
if not message.author.bot:
try:
await message.delete()
content = "Please use the following Format when posting:\n" \
"[Location] [H] [W]\n" \
"\nExample:\n" \
"[USA-CA] Narnia [H] Faun Hoof" \
"[W] Lions Tears.\n"
await message.author.send(content=content)
except:
pass
You can take the original message.content, save it, and append it to your new content as you please.
Beware though, consider what happens if someone sends a message containing #everyone. You should sanitize the user input.

How to add reactions to messages in discord.py without id

I am new to discord.py coding. I can't find the I'd for arrow up emoji... I've tried client.emojis and others but it doesnt work... pls help or send me all the ids or a website to see all the ids
just type your emoji in :this: form and stic an \ before the emojiname thats it!
when you send it it will come as <:something:1234567890123>
copy the numbers at the end thats your emoji id
same can be done with members and channels just instead of :this: do :#random_dude: and for channels do #general
hope this helps! Goodluck on your development!
I'm not sure what your code looks like, but if you're interacting with your 'message' asyncs you could do this.
emoji = '\N{THUMBS UP SIGN}'
# or '\U0001f44d' or '👍'
await message.add_reaction(emoji)
If you can, can you add your code so I can see where your error might be?
As zjbrown said, the thumbs up emoji can be stored in a string (and sent) using "\N{THUMBS UP SIGN}", "\U0001f44d", or "👍".
You can google a built-in emoji's name to find websites that have emoji descriptions. Sometimes these sites also have the unicode name, but this is not guranteed. If you want to send a custom emoji, you will have to find the emoji's id.
For built-in emojis, you can use RoboDanny's charinfo command (you'll have to add it to your own bot or use the RoboDanny bot in the discord.py support server) to get the unicode name.
If it is a custom emoji, you can send it in chat and get the image url (right click -> copy url). Then, look for the string of numbers before the .png and copy that. (Numbers only - don't copy the beginning of the url!) You can then send custom emojis by using the string <:[emoji name]:[emoji id]> for static emojis and <a:[emoji name]:[emoji id]> for animated emojis.

How can I properly upload too long messages as a file?

I was doing some research on Discord's capabilities today, and then I noticed that the "upload as file" option is given when a message is more than 2000 characters long.
The number plays a role here, but my statement can also be wrong, the fact is that some things are posted only as a link and or then not sent at all if I raise >2000.
Here now the problem:
I am requesting data from Genius for a lyrics command. I already have some kind of override built in for when len(data["lyrics"]) is greater than 2000 "characters", that should be Discord's limit after all, otherwise the bot wouldn't be able to process the embed I am trying to send.
How can I manage that lyrics >2000 are sent as a file?
Relevant code:
if len(data["lyrics"]) > 2000:
return await ctx.send(f"<{data['links']['genius']}>"))
This is the normal request, it works too, but it just throws out the link.
I then tried to send this via discord.File with the corresponding piece of code above, does not work. (Something like file=discord.File(data) was tried too!)
Something like ctx.file.send does not work in this case either
The following posts were viewed:
Uploading a file in a embed discord.py (not a image)
Discord.py - Sending a text file on DM
Discord.py Bot sending file to Discord Channel
Python discord.py ways to split output to bypass 2000 character limit
What I want my bot to do if data["lyrics"] > 2000:
You can save the text into a io.StringIO buffer and pass that into the discord.File constructor:
from io import StringIO
text = data["lyrics"]
if len(text) > 2000:
buffer = StringIO(text)
f = discord.File(buffer, filename="lyrics.txt")
await ctx.send(file=f)

Discord.py client.commands send mentioned user

I have this command called p!slap and I want it so that my bot will say {message.author} slapped {message.mention} and a random gif but I have no idea how to. Can anyone help? Thanks!
#client.command()
async def slap(ctx, message.mention):
embedVar = discord.embed(title=f"<#{message.author.id}> slapped {message.mention}")
list = [
#gif links and stuff
]
await ctx.channel.send(random.choice(list))```
Your code has a few problems.
At first, I couldn't understand what you're trying to do with naming a parameter message.mention but I guess what you're trying to do is "slapping" the person you mentioned in command. For that, you have to get the member object in parameter. Then, you can mention this member.
Also, you shouldn't define a variable named list. This might cause a error because of the built-in method list.
An another thing is, there's no embed method for discord module, it must be discord.Embed. You have to pay attention to the uppercase letters.
You never sent the embed you defined, you must send it for your command to work.
For the last one, I don't know what'll be in the list lst but I guess there will be files. If you're going to send a file, you cannot just send it. You have to pass a parameter in .send() method. But this is only for the files. If you're just going to send a link, then you don't have to pass any parameters.
#client.command()
async def slap(ctx, member: discord.Member):
embedVar = discord.Embed(title=f"{ctx.author.mention} slapped {member.mention}")
lst = [
#gif links and stuff
]
await ctx.channel.send(file=discord.File(random.choice(lst)), embed=embedVar)

Is there a way to pm an author and use the wait_for command to capture the value they type in?

I'm writing a discord bot using python to log options trade. What I'm looking to do with my bot is when a user type execute a command, !opentrade, the bot would private message the user with a series of questions. The bot would then capture the answer in a series of variables and use that to process the answer. Is this possible or does it have to be in the channel where the command is called. Additionally, is it by default multithread, meaning 2 or 3 people can call that command at the same time and it would fud up the answer
I have the following thus far and it isn't working:
async def opentrade(ctx):
def check(author):
def inner_check(message):
if message.author != author:
return False
try:
int(message.content)
return True
except ValueError:
return False
return inner_check
try:
await ctx.author.send('Enter the underlying: ')
underlying = await client.wait_for('message', check=check(ctx.author), timeout=30)
print (underlying)
except Exception as err:
await ctx.channel.send("Something went wrong: {}".format(err))
thanks
What I'm looking to do with my bot is when a user type execute a command, !opentrade, the bot would private message the user with a series of questions. The bot would then capture the answer in a series of variables and use that to process the answer. Is this possible or does it have to be in the channel where the command is called.
Possible. You need to put the whole logic into the coroutine that executes the command.
Additionally, is it by default multithread, meaning 2 or 3 people can call that command at the same time and it would fud up the answer
If you implement the checks properly it won't fud the answer. In your code you already implemented the logic that checks for the author so noone else can change it def check(author).
When executing the commands multiple times by different users it will also not fud the answer since the threads are independent.
You should extend this check so it checks for a message in a DM channel. The way your check is written now the caller can DM the bot or respond in a guild text channel.
I'm not sure what's saved in the ctx variable but I'm certain it has a message attribute.
Try await ctx.message.author.send(). If you are running into an error provide the error log.

Resources