Having trouble sending messages from a Discord Bot - python

I'm currently trying to make a Discord bot using Python, to automate some boring stuff. I'm currently just trying to make something that will respond to a message, and then send something back.
My code is Here:
import discord
TOKEN = '(The correct Token, hidden for obvious reasons)'
client = discord.Client()
#client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.content.startswith('!hello'):
msg = 'Hello {0.author.mention}'.format(message)
await client.send(message.channel, msg)
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run(TOKEN)
When I run this code, the bot appears online, and recognises when someone types !hello. However, Immediately after, it returns an error trying to send a message "AttributeError: 'Client' object has no attribute 'send'"
I've been at this for a fair few hours at this point, and any hep would be greatly appreciated

considering this has "!" at the beginning of it, instead of making this an on_message event, we can just turn this into a command by changing the following and make it more compact while doing so!.
original code:
import discord
TOKEN = '(The correct Token, hidden for obvious reasons)'
client = discord.Client()
#client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.content.startswith('!hello'):
msg = 'Hello {0.author.mention}'.format(message)
await client.send(message.channel, msg)
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run(TOKEN)
edited code:
import discord
from discord.ext import commands #imports the commands module
client = commands.Bot(command_prefix = '!')
#client.command()
async def Hello(ctx):
await ctx.send(f'Hello {ctx.message.author}')
#the rest is the same as your original code
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run('Token')
Hopefully that helps! Best of luck,
-Landon

Related

How can I make my Discord Bot send a message every-time a user types 'verify'

I have tried to search the web for ways to do this, but I've had no luck. I know this is seems very easy, but I am just starting with discord.py and I'm looking for some help. Thanks.
You can add this to your bot code.
#client.event
async def on_message(message):
if message.content == ‘verify’:
await message.channel.send(“A message”)
Here
#client.event
async def on_message(message):
if message.content == "verify":
#Your code
import discord
token = '<insert-token-here>'
# Make sure to enable the Message Content intent in the Discord developer portal
client = discord.Client(intents=discord.Intents.all())
#client.event
async def on_ready():
print(f"Logged on as {client.user}")
#client.event
async def on_message(message: discord.Message):
if message.author == client.user:
# skip message from self
return
print(f"Message: {message.content}")
await message.channel.send(message.content)
if __name__ == '__main__':
client.run(token)

Discord bot not responding (python)

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('token is here')
I'm trying to create a discord bot and I'm trying to use this simple code to get started but whenever I type the command ($hello) in a server that the bot is in it doesn't respond. Any solutions?
I'm assuming you're new to discord.py which is fine, though writing code like this is a little counter-intuitive. Instead of registering it as an event, why not just register it as an actual command?
This is how it should be done:
from discord.ext import commands
client = commands.Bot(command_prefix='$') # This can always be changed
#client.event()
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.command()
async def hello():
await ctx.send('Hello')
client.run('token is here')

Creating a discord bot with python. Cant make the bot do two tasks at once but the code works if only one task is being run

Im new to python so I cant see where I have made an error.
import discord
import os
from config import TOKEN
client = discord.Client()
#Sends a message in terminal if it has worked
#client.event
async def on_ready():
print('We have logged in as {0.user}'
.format(client))
#Sends a message if !test is included in a sent message
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!test'):
await message.channel.send('test')
#client.event
async def on_member_join(self, member):
guild = member.guild
if guild.system_channel is not None:
to_send = 'Welcome {0.mention} to {1.name}'.format(member, guild)
await guild.system_channel.send(to_send)
client.run(TOKEN)

discord bot not responding - python

I am working on a discord bot in python, and it has suddenly stopped responding. Any reason why this might be happening?
Here is my code;
import discord
TOKEN = '(censored)'
client = discord.Client()
prefix = '!'
playername = []
playercredits = []
#client.event
async def on_message(message):
if message.content.startswith(prefix + 'hi'):
msg = f'Hi baldy.'
await message.channel.send(msg)
#client.event
async def on_ready():
print('------')
print(client.user.name)
print(client.user.id)
print('------')
client.run(TOKEN)
if you have any idea why my bot isn't responding, please assist me. Thanks!
on_message is overriding the default provided on_message which forbids any extra commands from running. To fix this, add a client.process_commands(message) line at the end of your on_message. For example:
#client.event
async def on_message(message):
# do some extra stuff here
await client.process_commands(message)

why is on_raw_reaction_add never activated by a reaction in the discord server

I am writing a discord bot that adds your name to a list whenever you react to a certain message. Since these messages can be in the discord for a long time and my discord bot restarts from time to time I decided to use discord.on_raw_reaction_add(payload) instead of discord.on_reaction_add(reaction, user). This prevents the function from not activating when a message is not in the cache.
My problem is that, with the same code I had for discord.on_reaction_add(reaction, user), discord.on_raw_reaction_add(payload) is not activated.
Here are both simplified examples of my code. If you run it with a token and react to a (new) message you will see that the second sample does not print 'reaction added'.
Working version: (with on_reaction_add)
import discord
client = discord.Client()
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
#client.event
async def on_reaction_add(reaction, user):
print('reaction added')
client.run('TOKEN')
Broken version: (with on_raw_reaction_add)
import discord
client = discord.Client()
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
#client.event
async def on_raw_reaction_add(payload):
print('reaction added')
client.run('TOKEN')
I have tried to add reactions to old messages and new messages. Neither of them gave the expected 'reaction added' response.

Categories