discord.py Ping Command - discord

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**")

Related

How can I have a trigger without my command prefix, like welcome and not $welcome?

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!')

In discord.py when I use more than 1 `on_message` it does not works, only the last one works

This is my code, and on_message is not working when used twice, only the 2nd one is working. Please help me.
async def on_message(message):<br>
if message.content.startswith('-coinflip'):<br>
embedVar = discord.Embed(<br>
title="Toss",<br>
description=(f'You got {random.choice(heads_tails)}'),<br>
color=(0xFF0000))<br>
print(f'-coinflip command used by {message.author}')<br>
await message.channel.send(embed=embedVar)<br>
#client.event<br>
async def on_message(message):<br>
if message.content.startswith('-help'):<br>
embedVar = discord.Embed(<br>
title="Help arrived!",<br>
description="So, it looks like you need help, let me help you",<br>
colour=0xFF0000)<br>
embedVar.add_field(name="Bot Prefix", value="-", inline=False)<br>
embedVar.add_field(name="Moderation Commands",<br>
value="-help",<br>
inline=True)<br>
embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)<br>
embedVar.set_thumbnail(<br>
url=<br>
"https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif"<br>
)<br>
print(f'-help command used by {message.author}')<br>
await message.channel.send(embed=embedVar)<br>```
Here's the answer I wrote but couldn't post:
You cannot have 2 on_message event listeners. You can merge the two event listeners and their responses by using if...elif like this instead:
#bot.event
async def on_message(message): #When any message is sent
if message.content.startswith('-coinflip'):
embedVar = discord.Embed(
title="Toss",
description=f'You got {random.choice(heads_tails)}',
color=(0xFF0000))
print(f'-coinflip command used by {message.author}')
await message.channel.send(embed=embedVar)
elif message.content.startswith('-help'):
embedVar = discord.Embed(
title="Help arrived!",
description="So, it looks like you need help, let me help you",
colour=0xFF0000)
embedVar.add_field(name="Bot Prefix", value="-", inline=False)
embedVar.add_field(name="Moderation Commands",
value="-help",
inline=True)
embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)
embedVar.set_thumbnail(
url="https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif")
print(f'-help command used by {message.author}')
await message.channel.send(embed=embedVar)
elif is the same as else if; in this case, if the message's content doesn't start with "-coinflip" and starts with "-help", it creates and sends an Embed.
I've replaced the heads_tails variable for a fully-functioning code:
from discord.ext import commands
import discord
import discord.utils
import random
intent = discord.Intents(messages=True, message_content=True, guilds=True)
bot = commands.Bot(command_prefix="", description="", intents=intent)
heads_tails = ("Heads", "Tails") #Replace this with your sequence
#bot.event
async def on_ready(): #When the bot comes online
print("It's online.")
#bot.event
async def on_message(message): #When any message is sent
if message.content.startswith('-coinflip'):
embedVar = discord.Embed(
title="Toss",
description=f'You got {random.choice(heads_tails)}',
color=(0xFF0000))
print(f'-coinflip command used by {message.author}')
await message.channel.send(embed=embedVar)
elif message.content.startswith('-help'):
embedVar = discord.Embed(
title="Help arrived!",
description="So, it looks like you need help, let me help you",
colour=0xFF0000)
embedVar.add_field(name="Bot Prefix", value="-", inline=False)
embedVar.add_field(name="Moderation Commands",
value="-help",
inline=True)
embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)
embedVar.set_thumbnail(
url="https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif")
print(f'-help command used by {message.author}')
await message.channel.send(embed=embedVar)
Also, is "-help" a moderation command? And, try searching for and solving such problems yourself. StackOverflow shouldn't be the first place to ask such questions.

#client.command not running, discord.py [duplicate]

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")

!say discord.py rewrite command

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)

How can I make my bot say full sentences that I say?

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)

Resources