Why isnt my discord bot answering to my messages? - python

I made this small discord bot to show a friend how to do it, exactly how I have done it before. But it doesnt answer my message in discord, and I cant find the error.
import discord
import os
client = discord.Client()
#client.event
async def on_ready():
print('Online as {0.user}'.format(client))
#client.event
async def in_message(message):
if message.author == client.user:
return
if message.content.startswith('Hello'):
await message.channel.send('Hello! {message.author.mention}')
client.run(os.getenv('TOKEN'))
Sorry if its obvious, I just cant see it.

Use on_message instead of in_message.
Format string 'Hello! {message.author.mention}' like f'Hello! {message.author.mention}'.

instead of using on_message events u can use
#client.command()
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.mention}")
This is how u create actual Commands in discord.py

Related

I am running this code for a discord bot and while there is no error, it is not sending the desires message even after logging in

import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('your token here')
As mentioned above, I believe the issues come with the second async function or below, I tried testing it but there was no hello back. Thank you.
You ideally shouldn't be using discord.Client() here but discord.ext.commands.Bot()
Re-write :
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix="$")
#bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
#bot.command()
async def hello(ctx):
await ctx.send("Hello!")
bot.run(os.getenv("DISCORD_TOKEN"))
Which would result in :
Also, I can't see any issues in the code you provided other than the second if statement being incorrectly indented but even if that were the case you'll get an error.

Python Bot command not working but event is

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

Discord.py - bot doesn't respond

I'm building a Discord bot on Python and have an issue in code.
Here's my entire code:
import discord
from discord import message
from discord.ext import commands
client = commands.Bot(command_prefix='_')
gret_words = ['hi', 'grets', 'greetings', 'mornin', 'hey']
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.command(pass_context=True)
async def hello(ctx):
author = ctx.message.author
await ctx.send(f'Hello, {author.mention}! My name is Bot-3P0!')
async def on_message(message):
msg = message.content.lower()
if msg in gret_words:
await message.channel.send("Nice to see you!")
####################
client.run('TOKEN')
But my issue is that, when I type in messenger one word from the gret_words list, the bot literally doesn't react! I'll be grateful for all help!
You need to mark on_message as an event. Simply add #client.event on top of async def on_message(message) and it should work! Edit: you will need to add client.process_commands() to your on_message() as well

Get message author in Discord.py

I am trying to make a fun bot for just me and my friends. I want to have a command that says what the authors username is, with or without the tag. I tried looking up how to do this, but none worked with the way my code is currently set up.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
##gets author.
await message.channel.send('You are', username)
client.run('token')
I hope this makes sense, all of the code i have seen is using the ctx or #client.command
The following works on discord.py v1.3.3
message.channel.send isn't like print, it doesn't accept multiple arguments and create a string from it. Use str.format to create one string and send that back to the channel.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are {}'.format(message.author.name))
client.run('token')
Or you can just:
import discord
from discord import commands
client = commands.Bot(case_insensitive=True, command_prefix='$')
#client.command()
async def whoAmI(ctx):
await ctx.send(f'You are {ctx.message.author}')
If you want to ping the user:
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are', message.author.mention)
client.run('token')

I'm trying to learn how to make a discord bot and the example code it said to run doesn't work, what might be wrong?

I'm learning how to use the discord.py API and this example code (from here: https://discordpy.readthedocs.io/en/latest/quickstart.html) does not work. I think it has something to do with the async syntax, but I have no idea what could be wrong with it.
Any help would be appreciated!
import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('private token that is filled in in the actual code')
On line 7, “async def on_ready():”, the error is an invalid syntax on async. I’m using 3.8.3
client = commands.Bot(command_prefix = '!') instead of client = commands.Client()
You should import commands too
import discord
from discord.ext import commands

Categories

Resources