Reputation: 21
I'm making a repeat command with discord.py where you send in a command and it repeats the message you sent in. It works, but the only problem is that if I use spaces, for example "Hello I'm" it only prints out "Hello". How do I fix this?
Here is my code:
import discord
import hypixel
from discord.ext import commands
bot = commands.Bot(command_prefix='>')
@bot.event
async def on_ready():
print("Ready to use!")
@bot.command()
async def ping(ctx):
await ctx.send('pong')
@bot.command()
async def send(ctx, message):
channel = bot.get_channel(718088854250323991)
await channel.send(message)
bot.run('Token')
Upvotes: 2
Views: 5866
Reputation: 121
Change your code to the following:
@bot.command()
async def send(ctx, *, message):
channel = bot.get_channel(718088854250323991)
await channel.send(message)
This allows you to set multiple values into that same message. A better way to do this is:
@bot.command()
async def send(ctx, *, message:str):
channel = bot.get_channel(718088854250323991)
await channel.send(message)
This will ensure that the message value will be turned into a string. Always a good habit, because you don't know if you might make a typo and use it as another data type.
Upvotes: 0
Reputation: 75
Firstly, NEVER show your bot token publicly, this way anyone can write code for your bot and make it do whatever that person wants.
As to your question,
If you call the command with Hello I'm
, it'll only return Hello
. This is because, in your send function, you are accepting only one argument.
Thus, if you send Hello I'm
it's taking only the first argument passed to it which is Hello
. If you call that command again but this time with quotes, "Hello I'm"
for example, it will return Hello I'm
.
The solution for this would be changing your send function to something like this, which would take an arbitrary number of arguments and then join them together:
async def test(ctx, *args):
channel = bot.get_channel(718088854250323991)
await channel.send("{}".format(" ".join(args)))
Which would join all the arguments passed to it and then send that message.
As shown here Official Docs
Alternative: Using Keyword-Only Arguments: This can also be done as:
async def test(ctx, *, arg):
channel = bot.get_channel(718088854250323991)
await channel.send(arg)
Again, referring to the official docs at Keyword-only arguments
Upvotes: 5
Reputation: 1
ah this is easy
@client.command()
#* for infinite args
async def echo(ctx,*,args:str):# it will turn every thing into a str like echo hi , hi will be a str automatically
await ctx.send(args)
Upvotes: 0
Reputation: 11
Just write it like this:
@bot.command()
async def yourcommand (ctx,*,message):
await ctx.send(f"{message}")
Upvotes: 1