Pycord bot not responding to commands - python

I have a discord bot with automod and XP system. I have made discord bots before so I know how to make a command, but now it does not seem to work and I don't know why.
When I say a curse it deletes my message and everything there works fine so the on_message works, but those commands don't work. It does not give any error when I type a command, but the but doesn't respond.
Any help would be appreciated.
Here is my code:
import discord
from discord.ext import commands
# Set up the client and command prefix
intents = discord.Intents.all() # Enable all intents
client = commands.Bot(prefix='$', intents=intents)
#client.event
async def on_ready():
print('Bot ready')
#client.command()
async def ping(ctx):
print("ping")
await ctx.send('pong')
#client.event
async def on_message(message):
# Ignore messages sent by the bot
if message.author == client.user:
return
# Do some XP things
...
# AutoMod things
...
# Process any commands that are sent by the user
await client.process_commands(message)
print("processing commands")
#client.command()
async def leaderboard(ctx):
# do something
...
# Start the bot
client.run('TOKEN')
Also all intents are enabled in discord developer portal
So what I expected to happen is the program would first print Bot ready. Then I would write $ping to discord and the program would print processing commands and then print pong and say pong in discord.
What actually happened is that the program Printed Bot ready. Then I wrote $ping, the program prints processing commands. And then nothing else happens

Related

Discord Python bot commands not responding

basically i have no idea why this code is not working, ive tried using on_message and that works so long as i dont include and if statement to filter for the messages with the prefix at the start. so i scrapped that, this code worked a few months ago with a different bot i made and ive ended up scrapping all the other stuff i was doing and bringing it down to the bare basics because i cant figure out why the bot doesnt recognise messages starting with the prefix.
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
TOKEN = "Token_here"
#bot.command(name='tst')
async def test(ctx):
await ctx.send('testt')
#bot.event
async def on_ready():
print ('Successful login as {0.user}'.format(bot))
bot.run(TOKEN)
ive tried print('Test') debugging aswell see below
#bot.command(name='tst')
async def test(ctx):
print('Test")
then in discord typing the !test command still does nothing and the terminal also remains empty other than the on_ready result of
Successful login as botname#0001
i have no idea whats going on honestly
There are a couple of things that could be causing your problems. I fixed the issue by enabling intents for the bot. This also must be done on the developer portal. https://discord.com/developers/applications
intents = discord.Intents().all()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
you also don't need the name parameter in the command decorator. It can just be written as :
#bot.command()
async def test(ctx):
await ctx.send('testt')
Try these changes:
bot= commands.Bot(command_prefix='!!', intents=discord.Intents.all())
#bot.command()
async def test():
await ctx.channel.send("Test successful")

How to stop repetitive messages, and the token is changed, but it doesn't run

I started learning python today and made a Discord bot. I have a few problems:
If message.author == was used in the on_message, but the bot continued to reply to itself.
Afterwards, a new bot was created with a new token and the code didn't work.
I searched a lot on this site and Google. I didn't find any solution. It's okay to modify my code. Everything is from the internet, so it can be a mess. Please help me.
import discord
import asyncio
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_ready():
print('Loggend in Bot: ', bot.user.name)
print('Bot id: ', bot.user.id)
print('connection was succesful!')
print('=' * 30)
#client.event
async def on_message(message) :
if on_message.content.startswith('!의뢰'):
msg = on_message.channel.content[3:]
embed = discord.Embed(title = "브리핑", description = msg, color = 0x62c1cc)
embed.set_thumbnail(url="https://i.imgur.com/UDJYlV3.png")
embed.set_footer(text="C0de")
await on_message.channel.send("새로운 의뢰가 들어왔습니다", embed=embed)
await client.process_commands(message)
client.run("My bot's token.")
Your code was messy, but it should work now. I included comments to let you know how everything works. I think the good starting point to making your own bot is reading documentation. Especially Quickstart that shows you a simple example with explanation.
Write !example or hello to see how it works.
import discord
import asyncio
from discord.ext import commands
# you created 'client' and 'bot' and used them randomly. Create one and use it for everything:
client = commands.Bot(command_prefix="!") # you can change commands prefix here
#client.event
async def on_ready(): # this will run everytime bot is started
print('Logged as:', client.user)
print('ID:', client.user.id)
print('=' * 30)
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'): # you can change what your bot should react to
await message.channel.send("Hello! (This is not a command. It will run everytime a user sends a message and it starts with `hello`).")
await client.process_commands(message)
#client.command()
async def example(ctx): # you can run this command by sending command name and prefix before it, (e.g. !example)
await ctx.send("Hey! This is an example command.")
client.run("YOUR TOKEN HERE")

On member join event message doesn't work - discord.py

I'm very new to discord.py, I want to learn python and make a simple bot for my server.
I want make a bot that sends a message when someone joins server. But the bot won't send any message. I tried so many solutions but none work.
No error in console. Just 'on_member_join' doesn't work; ping and console message methods work.
Here is my code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='*')
TOKEN = '----my token here----'
#client.event
async def on_ready():
print('Bot is ready')
#client.event
async def on_member_join(member):
channel = client.get_channel(764960656553934879)
await channel.send('Hello')
#client.command()
async def ping(ctx):
await ctx.send('Pong!')
client.run(TOKEN)
For discord.py 1.5.0, reference the introduction of intents.
Your bot will need the members intent - which happens to be a priveleged intent - in order for the on_member_join event to be triggered properly.

How to make a discord bot react to PAST private messages?

I made a simple discord.py bot that reacts whenver it is PM'd. However, if someone messages it while my bot is offline, it will not react. How can i make it display all messages received while it was online on startup?
Current code:
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix='.')
#client.event
async def on_message(message):
if message.guild is None and message.author != client.user:
send_this_console=f'{message.author}: \n{message.content}\n'
print(send_this_console)
await client.process_commands(message)
You'll need to use message history and track the last message sent.
To get when a message was created:
lastMessageTimestamp = message.created_at
You'll need to store this in whatever way you want when your bot is offline (for example with pickle) and do this:
async for message in user.history(after = lastMessageTimestamp):
print(message.content)

Discord bot commands execute multiple times

It worked at first, but then somewhy it started to give the answers 4-5 times at once, any idea why?
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def once_ready():
print('Bot is ready.')
#client.event
async def on_member_join(member):
print(f'{member} has joined the server.')
#client.event
async def on_member_remove(member):
print(f'{member} has left the server.')
#client.command()
async def ping(ctx):
await ctx.send('Pong!')
client.run('my token here')
So, the problem was that I couldn't shut down the already running bots in Atom.
If you add the code that I wrote down there (that only the owner can
use) will shut down the already running bots (write /shutdown in
discord server or whatever your prefix is).
However, you may need a PC restart after saving the bot with this code.
#client.command()
#commands.is_owner()
async def shutdown(ctx):
await ctx.bot.logout()
So every time if you want to edit your command, you write /shutdown
and edit it, after that, you can start it again.
I hope this works for you and that I could help if you had the same problem.

Categories