Discord bot not responding (python) - 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')

Related

Discord bot does not respond to my commands

I'm a beginner to python and I am following a YouTube tutorial, and the problem is that the bot would not respond to any of the messages. Also I'm trying this on Replit. I would really appreciate your help.
import discord
import os
client = discord.Client(intents = discord.Intents.default())
#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(os.environ['TOKEN'])
You’re only using the default intents: you haven’t set the message content intents. See these for more info:
https://support-dev.discord.com/hc/en-us/articles/4404772028055-Message-Content-Privileged-Intent-FAQ
https://discordpy.readthedocs.io/en/stable/intents.html

How to execute discord.py loop task

This is my code but I cant get the loop to work.
import discord
from discord.ext import tasks, commands
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('!test'):
await message.channel.send('Working')
#tasks.loop(seconds = 10)
async def myLoop():
channel = client.get_channel(899698630271856640)
await channel.send('1')
To start a discord.py loop, you must run it under the on_ready function. Add myLoop.run() in the function.

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 Command is not Showing

So I'm making a basic bot command that responds with what the player said, like doing !test code will make the bot respond with 'code'. For some reason, nothing happens when the command is run. I even put a print inside of it to see if it was actually being run, and it wasn't. Here's my code:
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
#bot.command()
async def test(ctx, arg):
await ctx.send(str(arg))
client.run('token here')
Any help is appreciated.
try this out:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
#client.command()
async def test(ctx, *, arg):
await ctx.send(str(arg))
client.run('token here')
heres what you got wrong:
client = discord.Client()
bot = commands.Bot(command_prefix="!")
You had 2 separate handlers for the bot, if you use commands you only need the bot = commands.Bot(command_prefix="!") line, in this case you had the bot handler for commands but you were running client
The code below runs as expected when tested using Python3.7 ...
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
#client.command()
async def test(ctx, *arg):
await ctx.send(str(arg))
client.run('token here')
Your code as posted has an await statement outside of a function
......
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
.......
Also change
#bot.command()
async def test(ctx, arg):
TO:
#bot.command()
async def test(ctx, *arg):
For an explanation as to why you pass *arg and not arg:
args-and-kwargs
You can't run client and bot together if want bot to run the last line of code must be bot.run('your token') if you want client to run use client.run('your token')

Having trouble sending messages from a Discord Bot

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

Categories