This is my code
#bot.command()
async def say(ctx, message=None):
await ctx.message.delete()
await ctx.send(message)
However, if I do !!say I’m a dog, the bot will say only I’m
Can you help me please?
That's because space is an argument separator. If you want to have an argument with spaces use * in your function. Like this:
#bot.command()
async def say(ctx,*,message=None):
await ctx.message.delete()
await ctx.send(message)
Related
So I wanted to make a feature on my discord bot and I wanted it to respond when users say welcome and say Welcome! back. But every time I try it only responds to $welcome. ($ is my prefix). What should I do?
client = commands.Bot(command_prefix = "$")
#client.command()
async def welcome(ctx, message):
if any(word in message for word in welcome):
await message.channel.send("Welcome!")
#client.event
async def on_message(message):
if "welcome" in message.content:
await message.channel.send('Welcome!')
Basically, everything appears to work fine and start up, but for some reason I can't call any of the commands. I've been looking around for easily an hour now and looking at examples/watching videos and I can't for the life of me figure out what is wrong. Code below:
import discord
import asyncio
from discord.ext import commands
bot = commands.Bot(command_prefix = '-')
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.event
async def on_message(message):
if message.content.startswith('-debug'):
await message.channel.send('d')
#bot.command(pass_context=True)
async def ping(ctx):
await ctx.channel.send('Pong!')
#bot.command(pass_context=True)
async def add(ctx, *, arg):
await ctx.send(arg)
The debug output I have in on_message actually does work and responds, and the whole bot runs wihout any exceptions, but it just won't call the commands.
From the documentation:
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:
#bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)
The default on_message contains a call to this coroutine, but when you override it with your own on_message, you need to call it yourself.
Ascertained that the problem stems from your definition of on_message, you could actually just implement debug as a command, since it appears to have the same prefix as your bot:
#bot.command()
async def debug(ctx):
await ctx.send("d")
I have problem with my discord.py command. I want to make ping command which will respond with my bot's ping. My code is below.
#bot.command()
async def ping(ctx):
await ctx.send(f"Pong :ping_pong:! Bot latency: **{round(bot.latency * 1000)}ms**"
I also tried to write my code like this.
#client.command()
async def ping(ctx):
await ctx.send(f"Pong :ping_pong:! Bot latency: **{round(bot.latency * 1000)}ms**"
None of the above code works.
I have this command and I've tested it to know it works.. you can try it:
#bot.command()
async def ping(ctx):
await ctx.send(f'My ping is** {round(bot.latency*1000)} Ms**')
Btw, if you have defined bot as client, make sure to change bot.latency to client.latency
Did you add a bracket to the end?
#bot.command() # set this to your bots variable
async def ping(ctx):
await ctx.send(f"Ping!\nLatency is **{round(bot.latency * 1000)}ms**!")
embed = discord.Embed(
title="🏓 Pong!",
description=f"The bot latency is {round(self.bot.latency * 1000)}ms.",
color=0x42F56C
)
await ctx.send(embed=embed)
The problem with your code is that you forgot the ) between ms** and ".
#bot.command()
async def ping(ctx):
await ctx.send(f"Pong :ping_pong:! Bot latency: **{round(bot.latency * 1000)}ms**")
This is the code fixed, if you want to use #client, just use this:
#client.command()
async def ping(ctx):
await ctx.send(f"Pong :ping_pong:! Bot latency: **{round(client.latency * 1000)}ms**")
I have a mute command in my bot but i want to add a timer on it, not really sure how to tho, this is my current code. It adds the code correctly and it all sends correctly but the duration i'm not really sure about. Any help would be appreciated!
Edit: I got the duration working, but how would i convert it into minutes/hours etc.?
#client.command()
#commands.has_permissions(manage_messages=True)
async def mute(ctx, member: discord.Member, mute_time : int, *, reason=None):
role = discord.utils.get(ctx.guild.roles, name="[Muted]")
await member.add_roles(role)
await ctx.send(f'**Muted** {member.mention}\n**Reason: **{reason}\n**Duration:** {mute_time}')
embed = discord.Embed(color=discord.Color.green())
embed.add_field(name=f"You've been **Muted** in {ctx.guild.name}.", value=f"**Action By: **{ctx.author.mention}\n**Reason: **{reason}\n**Duration:** {mute_time}")
await member.send(embed=embed)
await asyncio.sleep(mute_time)
await member.remove_roles(role)
await ctx.send(f"**Unmuted {member.mention}**")
I use your code in minimal example and it's work well. What problems do you have? Example:
#client.command()
#commands.has_permissions(manage_messages=True)
async def mute(ctx, mute_time : int):
await ctx.send("Muted")
await asyncio.sleep(mute_time)
await ctx.send("Unmuted")
Exactly one minute passed. Result:
This is the code I am using currently:
#client.command()
async def say(ctx, arg: str):
await ctx.channel.purge(limit=1)
await ctx.send(f"{arg}")
This code works, but the bot only says the first word of the sentence. How can I make it say the whole sentence?
I hope this would work:
#client.command()
async def say(ctx, *args):
await ctx.message.delete()
# do you want to say it with TTS ?
await ctx.send(' '.join(args), tts=True)