1
votes

I am writing a simple discord bot for fun, and i want it to take (for example) a number from a message after certain phrase and use it later for example a certain user types "$test 6" and i want my bot to take that number ( 6 ) and for another example send it to the same channel.

i came up after creating this question to split the message.content will it work? (not counting that the user could write something else than numbers)

import discord

client = discord.Client()
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith("$test"):
        stringOne = str(message.content)
        stringTwo = "$test "
        split = stringOne.partition(stringTwo)[2]
        await message.channel.send("number/s from your message: " + split)

1

1 Answers

0
votes

This can be done with command arguments:

@bot.command()
async def test(ctx, number = None):
    if not number:
        await ctx.send("Please enter a number, for example: `$test 5`")
    else:
        await ctx.send(f"Here's the number you gave me: {number}")

And an example using some discord objects:

@bot.command()
async def kick(ctx, member: discord.Member = None, *, reason = "Default reason"):
    if not member:
        await ctx.send("Please provide a member for me to kick!")
    else:
        await member.kick(reason=reason)
        await ctx.send(f"Successfully kicked {member} for `{reason}`")

References: