I am making a discord chat bot. I want to set command "How are You?" But my program only choose 'How' . I need some suggestions.TIA
import discord
from discord.ext import commands
client = commands.Bot(command_prefix= '=')
#client.event
async def on_ready():
print('Bot is online')
#client.command()
async def hello(ctx):
await ctx.send('hello')
#client.command()
async def how_are_you(ctx):
await ctx.send('I am good')
client.run('token')
If you want to use a command, i don't think you can register one with spaces in it, however there are a couple of workarounds:
First you can use the first word of your string as the command name and then check for the content of the message:
#client.command()
async def how(ctx):
if ctx.message.content.lower() == "=how are you":
await ctx.send("I'm fine thanks")
else:
pass
The = prefix is needed in the message, replace it when you want to change your prefix.
Or, the second option would be to set up an on_message listener.
#client.event
async def on_message(message):
if message.content.lower().startswith("=how are you"):
await message.channel.send("I'm fine thanks.")
await client.process_commands(message)
Related
Commands not working but events are tried overriding my on_message but that didn't work. When I comment out the second client.event and down client.command works. Any idea of what I could be doing wrong? am I missing something?
import discord
from discord.ext import commands
import random
import time
from datetime import date
client = commands.Bot(command_prefix = '.')
#client = discord.Client()
#client.event
async def on_ready():
print('we have logged in as {0.user}'.format(client))
#client.command()
async def clr(ctx, amount=5):
await ctx.channel.purge(limit=amount)
#client.command(aliases =['should', 'will'])
async def _8ball(ctx):
responses =['As i see it, yes.',
'Ask again later.',
'Better not tell you now.',
"Don't count on it",
'Yes!']
await ctx.send(random.choice(responses))
#client.event()
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'):
await message.channel.send('Hello how are you?')
Based on the docs (the ones mentioned by moinierer3000 in the comments) as well as other questions on stack (listed below), on_message will stop your commands from working if you do not process the commands.
#client.event()
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'):
await message.channel.send('Hello how are you?')
await client.process_commands(message)
Other questions like this:
discord.py #bot.command() not running
Discord.py Commands not working because of a on_message event
Prefixed and non prefix commands are not working together on python discord bot
The command is executed, although the terminal says the command doesn't exist.
Not sure what's up as this is my first discord bot, I know a little bit of python, so I am sorry for taking up your guys's time, really.
Thank you for the help though!
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix= ':')
game = discord.Game("with the API")
#list of banned words
filtered_words = ['bad words']
#bot.event
async def on_ready():
print("Hello I am online and ready!")
bot.run('token')
#Group message clearing
#bot.command(aliases=['c'])
#commands.has_permissions(manage_messages = True)
async def clear(ctx, amount=1):
await ctx.channel.purge(Limit = amount)
await ctx.reply(f'Deleted {clear} messages')
#auto mod
#bot.event
async def on_message(msg):
for word in filtered_words:
if word in msg.context:
await msg.delete()
await bot.process_commands(msg)
#bot.event
async def on_disconnect():
print("I seem to be getting disconnected, one minute!")
#bot.event
async def on_member_join(memeber):
print(f'Weclome {memeber} to the server, please enjoy your stay!')
#bot.event
async def on_message_delete(msg):
msg.send(" <:raysQ:835686467844964395> ")
The bot.run part should be at the end of the file. It's.the entry point to the loop.
Also please regenerate your token before someone else can use it.
I've started to try coding a discord bot but it was cut short when setting a prefix and events wouldn't work. It just displays an error message that says 'the command "hello" can not be found'.
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix="*" ,status=discord.Status.idle, activity=discord.Game("Starting up..."))
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.do_not_disturb, activity=discord.Game("Preparing Puzzle Event"))
print("Bot is running.")
#client.event
async def hello(ctx):
await ctx.send("Hi")
client.run('[The token]')
I know that the token is valid as I've ran it before without assigning a prefix and using 'async'.
#client.event
async def hello(ctx):
await ctx.send("Hi")
This won't work because hello is not a built-in event. See: https://discordpy.readthedocs.io/en/latest/api.html#discord-api-events
What you want is a command probably
#client.command()
async def hello(ctx):
await ctx.send("Hi")
This registers the bot to respond to the command .hello assuming . is your prefix
i need to use my discord command in specific channel , I'm using #client.command()
I mean if i used my command in another channel will be not working
#client.command()
async def w(ctx):
channel = client.get_channel(775491463940669480)
await ctx.send("hello) ```
You can check if ctx.channel is equal to your specific channel.
#client.command()
async def w(ctx):
if ctx.channel.id == 775491463940669480:
await ctx.send("hello)
There is even an easier way to do it without getting the channel. And without having to indent the whole code.
#client.command()
async def w(ctx):
# exit command if not the desired channel.
if ctx.channel.id is not 775491463940669480:
return
#code here
await ctx.send("hello")
client = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print("bot is Online")
-
#client.event
async def on_message(message):
cmdChannel = client.get_channel(776179059355942943)
if message.content.lower().startswith('!w'):
if message.channel.id == cmdChannel.id:
#command invoked in command channel - execute it
await client.process_commands(message)
else:
#command attempted in non command channel - redirect user
await message.channel.send('Write this command in {}'.format(cmdChannel.mention))
#client.command()
async def w(ctx, amount):
await ctx.send(f"done {amount}")
async def is_channel(ctx):
return ctx.channel.id = 775491463940669480
#client.command()
#client.check(is_channel)
async def w(ctx):
await ctx.send("hello)
Making the check call the function is_channel makes the code simpler and you can resuse it multiple times.
As described in the title, I am new to Python(programming in general) and I tried making a bot, however the bot does not respond to commands. I followed/looked through multiple youtube tutorials & articles, but I cannot find a way to fix my problem.
import discord
from discord.ext.commands import Bot
bot = Bot(".")
#bot.event
async def on_ready():
print("kram is now online")
await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))
#bot.event
async def on_message(message):
if message.author == bot.user:
#bot.command(aliases=["gp"])
async def ghostping(ctx, amount=2):
await ctx.send("#everyone")
await ctx.channel.purge(limit = amount)
#bot.command()
async def help(ctx):
await ctx.send("As of right now, .gp is the only working command.")
bot.run("I'm hiding my token")
Hey why don't you try this instead the same thing but i removed the on message
import discord
from discord.ext.commands import Bot
bot = Bot(".")
#bot.event
async def on_ready():
print("kram is now online")
await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))
#bot.command(aliases=["gp"])
async def ghostping(ctx, amount=2):
await ctx.send("#everyone")
await ctx.channel.purge(limit = amount)
#bot.command()
async def help(ctx):
await ctx.send("As of right now, .gp is the only working command.")
bot.run("I'm hiding my token")
This should work as when using cogs and when you have a command there is no need to put it in the on_message event. i suggest you watch this series(Really helpful while starting out):
https://www.youtube.com/watch?v=yrHbGhem6I4&list=UUR-zOCvDCayyYy1flR5qaAg