Reputation: 31
So I'm making a guess the number bot. I'd like the bot to send when the number is guessed and then pin the message of what the bot sent. How can I do this?
@client.event
async def on_message(message):
if message.content == str(number) and message.channel.id == 555146955815256065:
author = message.author.id
await message.channel.send("Congratulations <@" + str(author) + ">! You guessed the number, `" + str(number) + "`!")
await message.pin()
I've done this and this will pin the message the user sent, not the bots message,
Any help is appreciated, thanks!
Upvotes: 3
Views: 1441
Reputation: 791
You have to define the message sent by the bot (For example here as botmsg
) and pin this. Otherwise it pins the message defined in on_message(message)
what is the message on what the bot reacts.
Also I overworked the message itself so it is much easier to mention the user and insert the number (which you could also do as an integer but string also works).
@client.event
async def on_message(message):
if message.content == str(number) and message.channel.id == 555146955815256065:
botmsg = await message.channel.send(f"Congratulations {message.author.mention}! You guessed the number, `{number}`!")
await botmsg.pin()
Upvotes: 1
Reputation: 327
The first if
statement is used so the message does not loop
@bot.event
async def on_message(msg):
if msg.author.id != bot.user.id:
message=await msg.channel.send("You guessed it")
await message.pin() #Pins the message the bot sent
await msg.pin() #Pins the message the user sent
Upvotes: 0
Reputation: 139
In your original code your reference is not the message your bot sends but it is the message the user sends so it is obvious that the users message will be pinned.
To fix it you need to catch the message your bot sends too.
So you could do:
@client.event
async def on_message(message):
if message.content == str(number) and message.channel.id == 555146955815256065:
author = message.author.id
await message.channel.send("Congratulations <@" + str(author) + ">! You guessed the number, `" + str(number) + "`!")
if message.id== botid and message.channel.id == 555146955815256065:
await message.pin()
If there are problems catching right messages you can put filters on that too to check if it starts with "Congratulations"
using regex.
Upvotes: 0