can't receive the message.content with discord bot - python-3.x

Hello I began a discord bot but I can't give the message's content. I managed to get all the other things about message but the content return nothing. here is the code(ps: sorry if my english isn't very good, i'm French) :
import discord
class MyClient(discord.Client):
async def on_ready(self):
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
async def on_message(self, message: discord.message.Message):
# we do not want the bot to reply to itself
print(message.id)
if message.author.id == self.user.id:
return
if message.content.startswith('!hello'):
print('ok')
await message.channel.send('Hello {0.author.mention}'.format(message))
defIntents = discord.Intents.default()
defIntents.members = True
client = MyClient(intents=defIntents)
client.run('token')

You have to enable the Message Content Intent of your application from Discord Developer Portal and in your code you have to set the message_content intent to True like this.
defIntents = discord.Intents.default()
defIntents.members = True
defIntents.message_content = True
client = MyClient(intents=defIntents)

Related

Discord bot bug not responding

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").

Discord bot stickers

Is there a way that I can get my discord bot to send stickers after somebody says a certain thing, I got normal messages and emojis to work while it replies but there is not much learning material to find out how to get stickers to work with discord.py.
This is my code :
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='.nerd ', description = 'nerd eyes')
#bot.event
async def on_ready():
guild_count = 0
for guild in bot.guilds:
print(f"- {guild.id} (name: {guild.name})")
guild_count = guild_count + 1
print('Nerdeyes has awoken in ' + str(guild_count) + " servers")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith('nerd'):
msg = 'eyes'
await message.channel.send(msg)
if message.content.startswith('guy'):
msg = '<:guysusthink:873066296558882847>'
await message.channel.send(msg)
bot.run("TOKEN")
Stickers are a new Discord feature and haven't been added yet in Discord.py. You're gonna have to wait a bit until they release the new version.

Why does my on_message event doesn't work (discord.py)

I'm making a discord bot that worked fine but I wanted to start using cogs because I thought it was a nicer way to write my code but now my on_message doesn't work any more and it doesn't show any error messages I searched how to fix it in the whole internet and all explanations didn't work for me so I decided to ask here. So here is my code:
from discord.ext import commands
TOKEN = 'my token'
bot = commands.Bot(command_prefix="$")
class Interactions(commands.Cog):
#commands.Cog.listener()
async def on_message(self, message):
msg = message.content
hi = ["hi", "hello", "hi!", "hello!"]
bye = ["bye", "good bye", "bye!", "good bye!"]
# Messages in english
if str(msg).lower() in hi:
await message.channel.send('Hello!')
if str(msg).lower == 'how are you?':
await message.channel.send("I'm fine")
if str(msg).lower == "what's your favorite food?":
await message.channel.send("Sushi with sweet potato!")
if str(msg).lower == "what do you eat?":
await message.channel.send(
"I don't eat, I'm just a simple bot :pensive:")
if str(msg).lower == "are you fine?":
await message.channel.send("Yes, I am")
if str(msg).lower in bye:
await message.channel.send("Good bye!")
def run():
bot.add_cog(Interactions(bot)
bot.run(TOKEN)
if __name__ == "__main__":
run()
Import:
from discord.ext.commands import command, Cog
Rest of the code:
TOKEN = 'my token'
bot = commands.Bot(command_prefix="$")
class Interactions(commands.Cog):
#commands.Cog.listener()
async def on_message(self, message):
pass
This should work, if not just ping me ;)

Discord.py repeat message

i write bot on discord py with cogs.
main:
intents = discord.Intents.all()
client = discord.Bot(command_prefix = '$', intents = intents)
for files in os.listdir('./cogs'):
if files.endswith('.py'):
client.load_extension(f'cogs.{files[:-3]}')
token = os.environ.get('token')
client.run(token, bot=True)
and cog:
class textCommands(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command()
async def ping(self, ctx):
await ctx.send(f'Pong! {round(self.client.latency * 1000)}ms')
def setup(client):
client.add_cog(textCommands(client))
And my bot send two messages if i send 'ping', i changed my token, but problem still have.
so, I found the answer. I remove await self.client.process_commands(message) from my code and twices messages lose

Python how to forward messages from server

Hello i have a old discord server which we don't using now. and we created a new server and moved all members there but still some members r there in old server. So is there a option to forward all messages from server A to server B to specific channel.
Update:
I mean like when ever server A gets a message it should be sended to server B to a specific channel. bot is in both server so with that i can forward all incoming message exactly.
Bot Code
token = "xxxxxxxxxxxxx"
prefix = "!"
import discord
from discord.ext import commands
from discord.ext.commands import Bot
bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")
#bot.event
async def on_ready():
print('\nLogged in as')
print("Bot Name: " + bot.user.name)
print("Bot User ID: " + bot.user.id)
old_server = bot.get_server('xxxxxxxxxxxxx')
new_channel = bot.get_channel('xxxxxxxxxxxxx')
#bot.event
async def on_message(message):
message.content = message.content.lower()
if message.server == old_server:
await bot.send_message(new_channel, message.content)
await bot.process_commands(message)
bot.run(token)
You can use the on_message event to check when messages are sent to the old server and have the bot post the message to the new server.
Below is example code where the bot will check when the old server gets a message, then posts the same message to a specified channel on the new server.
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.event
async def on_message(message):
old_server = client.get_server('old_server_id')
new_channel = client.get_channel('new_channel_id')
if message.server == old_server:
await client.send_message(new_channel, message.content + ' - ' + message.author.nick)
client.run('token')

Resources