I'm trying to make a discord bot that will take a twitter link posted by a user, copy it, add VX and posts it so it embeds properly, and deletes the original message.
I cant seem to figure out the best way to have the bot copy the text, add "vx" before "twitter" and posting that. Like so https://twitter.com/i/status/xxxx > https://vxtwitter.com/i/status/xxxx
This is what I have
@bot.event
async def on_message(message):
if message.content == 'https://twitter.com':
messages=await message.channel.send("https://vxtwitter.com")
await asyncio.sleep(INT)
await messages.edit(content="https://vxtwitter.com")
I also cant seem to get ctx to work in my code, I'm not sure if it would help me to get it working. Any help is appreciated thanks in advance.
For your problem to be solved, you need to walk through the length of the content. Lucky for you, you know the exact place of where to put "vx".
Code:
@bot.event
async def on_message(message: discord.Message):
if "https://twitter.com/" in message.content:
new_message = message.content[:8] + "vx" + message.content[8:]
await message.channel.send(new_message)
await message.delete()
await bot.process_commands(message)
Explanation:
You know you want to put "vx"
before "twitter.com/"
and after "https://"
. After "twitter.com/"
you don't know how many characters the user will type. So we use the "https://"
which is a fix length. That's 8 letters.
For the new message we want https:// + vx + twitter.com/...
so we include the first 8 letters of the original message: message.content[:8]
then add "vx"
then the rest of the original message: message.content[8:]
the [:8]
means everything before the 8th letter and the 8th itself
the [8:]
means everything after the 8th letter
The bot.process_commands(message)
is for the ctx
problem. If you don't put this line in an on_message
event the bot will get stuck trying to do something with the command as a message. With this line it will first check if your message is a command.
I think that's it. If you have questions, I'll try and answer them.