I am trying to make a bot that responds to certain keywords but for some reason I can't seem to get it to work. E.g. if I were to say "This game is so hard" it would respond with "Hehe: Hard". (Childish I know). How would I edit this code to make it work because at the moment it refuses to work. thank you
#client.event
async def on_message(message):
if "hard" in message.content.lower():
await message.channel.send('Hehe "Hard"')
return
Where is this supposed to happen? In a server? Do other functions work, is your bot functioning at all? This code seems to have worked for me:
#client.event
async def on_message(message):
if not message.author.bot and "hard" in message.content.lower():
await message.channel.send('Hehe "Hard"')
Would it be possible for you to send all of the code you currently have?
Related
why does it's not working for me?
#client.event
async def on_member_join(member):
await client.get_channel(965214301520560178).send(f"{member.name} has joined")
thanks for the helpers!
I tried to make a welcome command in discord.py
If you want to ping the new member and that is the part that doesn't work (a bit unclear) it should be:
await client.get_channel(965214301520560178).send(f"<#{member.id}> has joined")
also, make sure to check if the bot has the necessary permissions and/or intents
I've just gotten into writing discord bots. While trying to follow online instructions and tutorials, my bot would not respond to commands. It responded perfectly fine to on_message(), but no matter what I try it won't respond to commands. I'm sure it's something simple, but I would appreciate the help.
import discord
from discord.ext.commands import Bot
from discord.ext import commands
bot = commands.Bot(command_prefix='$')
TOKEN = '<token-here>'
#bot.event
async def on_ready():
print(f'Bot connected as {bot.user}')
#bot.event
async def on_message(message):
if message.content == 'test':
await message.channel.send('Testing 1 2 3')
#bot.command(name='go')
async def dosomething(ctx):
print("command called") #Tried putting this in help in debugging
await message.channel.send("I did something")
bot.run(TOKEN)
Picture of me prompting the bot and the results
I made the same mistake at first.
#bot.event
async def on_message(message):
if message.content == 'test':
await message.channel.send('Testing 1 2 3')
This function overiding the on_message event so it is never sent to bot.command()
To fix it you just have to add await bot.process_commands(message) at the end of the on_message function:
async def on_message(message):
if message.content == 'test':
await message.channel.send('Testing 1 2 3')
await bot.process_commands(message)
Haven't tested yet but that should fix your issue.
Ok. First of all, the only import statement that you need at the top is from discord.ext import commands. The other two are not necessary.
Second of all, I tried messing around with your code myself and found that the on_message() function seems to interfere with the commands so taking that out should help.
Third of all, I only found this out when I duplicated one of my own working bots and slowly changed all of the code until it was identical to yours. For some reason python didn't like it when I just copied and pasted your code. I have never seen something like this before so I honestly don't know what to say other than that your code is correct and should work as long as you take the on_message() function out.
Here's the final code that I got working:
from discord.ext import commands
bot = commands.Bot(command_prefix="$")
TOKEN = "<token-here>"
#bot.event
async def on_ready():
print(f'Bot connected as {bot.user}')
#bot.command()
async def dosomething(ctx):
await ctx.send("I did something")
bot.run(TOKEN)
As you can see the only things that I have changed from your code are that I removed the redundant imports at the top, and I deleted the on_message() function. It works perfectly like this on my end so I would suggest that you re-type it out like this in a new file and see if that works.
If that doesn't work for you then my next guess would be that there is a problem with your installation of discord.py so you might try uninstalling it and then reinstalling it.
If none of that helps let me know and I will see if I can help you find anything else that might be the cause of the problem.
just put client.process_commands(message)
in on_message event at last..
TIA for your help and apologies, I'm a newbie so this may be a foolish question. I've searched through and can't find anything specific on how to make a discord bot (in Python) delete messages only within a specific channel. I want all messages sent to a specific channel to be deleted, their contents sent via PM to the user and the user's role changed.
Is there a way to use on_message and specify in an specific channel?
#client.event
async def on_message(message):
user = message.author
if message.content.startswith("Cluebot:"):
await message.delete()
await user.send("Yes?")
await user.remove_roles(get(user.guild.roles, "Investigator"))
The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?
Is it possible to make this work through a command instead of an event?
Thanks for your help. :)
The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?
About this problem the clue is:
await bot.process_commands(message)
as docs says Without this coroutine, none of the commands will be triggered.
about the main question you could try using get_channel by ID and then purge it:
eg.
#client.event
async def on_message(message):
purgeChannel = client.get_channel([CHANNEL ID]])
await purgeChannel.purge(limit=1)
you can add check to purge() to check for deleting specific message:
eg.:
def check(message):
return message.author.id == [author's ID]
#client.event
async def on_message(message):
purgeChannel = client.get_channel([CHANNEL ID]])
await purgeChannel.purge(limit=1, check=check)
So I am trying to create an event in discord.py where, whenever a user mentions/pings the bot, it will say something. The code below runs without errors, however the only way this will work is if my command prefix is in the message.
For my bot, the prefix is "/", so whenever I mention the bot with a "/" in the message it will say something. And if I decide to just mention the bot, the bot does not respond. I am pretty sure it's got something to do with the last line of code but I don't know how to fix this issue.
#client.event
async def on_message(message):
if client.user.mention in message.content.split():
await message.channel.send('You mentioned me!')
else:
await client.process_commands(message)
The code is written in Python 3.7.4.
All help would be appreciated!
When someone mentions a user the bot reads <#userid>. So if you want to use an on_message function you would want to include an
if '<#bot/user id>' in message.content:
await message.channel.send('hi')
You can also do something like...
if bot.user in message.mentions:
await message.channel.send('hi')
I'm really new to coding, and I'm just wondering if I can get some help with a discord bot I've based off of Frost Bot.
The idea is pretty simple, Frost will respond to users, but I've removed the need to mention it to get a response.
I have a twin bot, too, and the idea is to get them to converse, which they do, however it seems that they're trying to respond to what they themselves have said, which causes a backlog of replies to come out.
Here is the bulk of the code, written in Python- if that matters.
#client.event
async def on_ready():
print('Logged in as '+client.user.name+' (ID:'+client.user.id+') | '+str(len(client.servers))+' servers')
await client.change_presence(game=discord.Game(name='chat with me!'))
#client.event
async def on_message(message):
if not message.author.bot == client.user:
await client.send_typing(message.channel)
txt = message.content.replace(message.server.me.mention,'') if message.server else message.content
r = json.loads(requests.post('https://cleverbot.io/1.0/ask', json={'user':user, 'key':key, 'nick':'frost', 'text':txt}).text)
if r['status'] == 'success':
await client.send_message(message.channel, r['response'] )
Any help would seriously be appreciated, as I'm incredibly new to scripting/coding and have spent a few hours already trying to research a solution.
Do a message.author.id == otherBotID: #stuff instead.
If you have more than one other bot to have a conversation with, switch to a NOT operator and place it as your own bot's ID, then check if the message author is a bot.
EDIT
Replace it in if not message.author.bot == client.user:.
Originally, you were checking if the message's author is not a user. And if it was not a user you would do stuff.
But since your own bot is not a user itself, the if statement would pass too when your bot sends a message (and recieves it's own message).
Hence we are placing a if-statement to check if the bot's ID equals to the targeted bot's ID.