Discord bot bug not responding - python-3.x

Hello im new to stackoverflow i am developping a discord bot
but it does not react to commands in my dicsord server only in mp
here is my code
`
from discord.ext import commands
TOKEN = "X"
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_ready():
print(f'{bot.user} succesfully logged in!')
#bot.event
async def on_message(message):
# Make sure the Bot doesn't respond to it's own messages
if message.author == bot.user:
return
if message.content == 'hello':
await message.channel.send(f'Hi {message.author}')
if message.content == 'bye':
await message.channel.send(f'Goodbye {message.author}')
await bot.process_commands(message)
#bot.command()
async def test(ctx, *, arg):
await ctx.send(arg)
def to_upper(argument):
return argument.upper()
#bot.command()
async def up(ctx, *, content: to_upper):
await ctx.send(content)
bot.run(TOKEN)
`
please help me
IN a server :
in a mp:
I tried a lot of thing
but nothing works i am making a nice bot for my friends and i am a noob for discord.py

ok, so, multiple #bot.event can be an issue, especially if you're looking for messages.
You need to set a listener.
I also see you do not have intents set up, so you also need to do that.
when looking for messages, you'll want something like this:
#bot.listen()#this makes the bot only listen for the messages
async def on_message(message):
# Make sure the Bot doesn't respond to it's own messages
if message.author == bot.user:
return
if message.content == 'hello':
await message.channel.send(f'Hi {message.author}')
if message.content == 'bye':
await message.channel.send(f'Goodbye {message.author}')
And just as an additional tip: use ctx.channel.send("message") instead of ctx.send("message").

Related

Discord.py not responding to #client.event commands

I am not getting a "ping" response with this code. It was working before, but I am not sure what changed. There are no errors on my end, just no response.
Any feedback is appreciated.
import os
import random
import discord
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
PREFIX = os.getenv("PREFIX")
TOKEN = os.getenv("TOKEN")
intents = discord.Intents().all()
bot = commands.Bot(command_prefix=PREFIX, intents=intents)
#bot.event
async def on_message(message):
if message.author == bot.user: # tells the bot not to respond to itself
return
#bot.event # ping-with-latency
async def on_message(message):
if message.content.startswith(PREFIX + 'ping'):
await message.channel.send(f'pong! {bot.latency}ms')
#bot.event
async def on_ready(): # display if online/ready
print("Bot is ready and logged in as {0.user}!".format(bot))
# run bot on server
bot.run(TOKEN)
I have checked all permissions and privileged gateway intents. I know I could be using client.command, but that also doesnt work.
You're defining two different callbacks for these events - this is probably the problem. Just put the author check in the main on_message.
#bot.event
async def on_message(message):
if message.author == bot.user: # tells the bot not to respond to itself
return
if message.content.startswith(PREFIX + 'ping'):
await message.channel.send(f'pong! {bot.latency}ms')

Make a bot command

I'm actually trying to make a bot with discord.py for me and my friends, just for fun and practice python, but, when i try to make a bot command, that can be use with prefix + [name_of_the_command], it doesn't work.
import discord
from discord.ext import commands
intents = discord.Intents(messages=True, guilds=True)
intents.message_content = True
intents.messages = True
intents.guild_messages = True
bot = commands.Bot(command_prefix="$", intents=intents)
#bot.event
async def on_ready():
print(f'Logged on as {bot.user}!')
#bot.event
async def on_message(message):
print(f'Message from {message.author}: {message.content}')
# if message.author == bot.user :
# return
# if message.channel == discord.DMChannel:
# return
#bot.command()
async def ping(ctx):
print("test")
await ctx.channel.send("pong")
bot.run('MY_BOT_TOKEN')
I search on lot's of tuto, website, forum ... to see if there is an explaination but i didn't found, so expect someone could tell me what i make wrong to this simple thing doesn't work; idk if it's change something but i'm using python 3.11.1 and the last update of discord.py (i just dl discord.py today)
That what i get in my cmd, as you can see, i have the message content, but after, that make nothing
ty and have a nice day
You have to process the command in the on_message event by using process_commands:
import discord
from discord.ext import commands
intents = discord.Intents(messages=True, guilds=True)
intents.message_content = True
intents.messages = True
intents.guild_messages = True
bot = commands.Bot(command_prefix="$", intents=intents)
#bot.event
async def on_ready():
print(f'Logged on as {bot.user}!')
#bot.event
async def on_message(message):
print(f'Message from {message.author}: {message.content}')
await bot.process_commands(message)
# if message.author == bot.user :
# return
# if message.channel == discord.DMChannel:
# return
#bot.command()
async def ping(ctx):
print("test")
await ctx.channel.send("pong")
bot.run('MY_BOT_TOKEN')

Delete discord invites if someone post it in a channel

I want that the bot remove invites from channel if someone send one in all channels
Im using discord.py 1.6.0 and wrote it so but nothing happens.
rules = xxx
links = xxx
teamroom = xxx
# regex
DISCORD_INVITE = r'discord(?:\.com|app\.com|\.gg)[\/invite\/]?(?:[a-zA-Z0-9\-]{2,32})'
class Mod(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.links_allowed = (rules, teamroom, links)
self.url_regex = DISCORD_INVITE
#commands.Cog.listener()
async def on_message(self, message):
if search(self.url_regex, message.content):
await message.delete()
await message.channel.send("You cant send invites.", delete_after=10)
def setup(bot):
bot.add_cog(Mod(bot))
Might be late but try this:
import re
discordInviteFilter = re.compile("(...)?(?:https?://)?discord(?:(?:app)?\.com/invite|\.gg)/?[a-zA-Z0-9]+/?")
async def on_message(message):
if discordInviteFilter.match(message.content):
await message.delete()
await message.channel.send('no invites NOPPERS')
await client.process_commands(message)
You can check if the link posted starts with https://discord.gg and delete the message if the condition is met.
async def on_message(self, message):
if message.content.startswith("https://discord.gg"):
await message.delete()
await message.channel.send("You cannot send invites.")
You can check if the message has discord.gg in it I think having even if the message doesn't have HTTPS it still works with discord.gg if you add HTTPS:// then users will bypass by just using discord.gg only Hope you got your answer

Discord bot using discord.py command not working.PLZ

I just start learning how to write my own bot in discord.py. However, I followed the tutorial on Youtube, using exactly the same code but it doesn't work on my server.
import discord from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.command(pass_context = True)
async def test(ctx):
await ctx.send('Pong!')
#client.event
async def on_ready():
print('Bot is ready')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'):
await message.channel.send('Hello there.')
#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'Seeya,{member}. Oh wait\n can we?')
client.run('NzU1MjI2Mzk1MjM5Nzc2Mjg2.XGGNZA.s2qLVQMONMLqs1f8Zky9wtM6ZDo')
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a client.process_commands(message) line at the end of your on_message.
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'):
await message.channel.send('Hello there.')
await client.process_commands(message)
Why does on_message make my commands stop working?

AttributeError: 'NoneType' object has no attribute 'send' Discord.py rewrite

I'm trying to send a message to a specific channel. I have still not been able to figure out how to do this. Help is greatly appreciated.
import keep_alive
import discord
client = discord.Client()
channel = client.get_channel('ID'`enter code here`)
#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 channel.send('hello')
keep_alive.keep_alive()
client.run('ID')
get_channel won't work where you have it because the bot hasn't connected to discord yet (that happens during run). When the bot connects, it will build internal caches of all the things it's aware of (members, guilds, channels, etc.). Those caches are what the various get methods use, but since the caches are empty those methods return None.
Instead, you can either get the channel in the on_message every time, or use a global variable in the on_ready:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
channel = client.get_channel(1234)
await channel.send('hello')

Resources