Reputation: 11
So I'm trying to make a discord bot with python, and I would like it to ping a certain role, adding parts of your message into specific places. Such as: @role, Sponsor: @person, Message: message.
This is my code;
if message.content[11:] != message.content:
if message.content[10:] != "@Johnny Wobble#1085":
print("confirmed stage 2")
responses = [
"<@&820375570307088404>",
f"Sponser: {message.content[10:]}",
f"Message: {message.content[11:]}",
]
await message.delete()
await message.channel.send(responses)
else:
print("confirmed stage 3")
await client.send_message(message.channel, f"Ah, I see you {message.author.mention}, trying to turn me agai"
f"nst my master eh? Well I say no! I cannot believe you would think that I would ever do that to the all-po"
f"werful Max (Gordon)!")
I tried this code but it just ends up repeating the entire message.
For example: -gw @person pls work
Then it returns '@role' 'Sponsor: @person pls work' 'Message: @person pls work'
Do I have some problems in my code?
Upvotes: 1
Views: 5833
Reputation: 2346
I would suggest using the commands extension rather than using an on_message
event. If you're already doing that and I misread your question, please view the next section of this answer.
from discord.ext import commands # unsure if importing is necessary, usually used for cogs
# ...
# set up code with client and on_ready and what not
# ...
@client.command()
async def sponsor(ctx, role: discord.Role, *, message="Sponsor message"):
# get the role if given is valid, message is 'Sponsor message' if nothing is given
await ctx.send(f"""
{role.mention}
Sponsor: {ctx.author.mention}
Message: {message}
""")
# role.mention : mentions the given role
# sponsor : mentions the person who originally invoked the command
Assuming that you are using the commands extension already, you may use client.wait_for
. This is used if you want your bot to listen to the channel for a specific message under a command.
@client.command()
async def sponsor(ctx):
def check(message):
return message.author == ctx.author and message.channel == ctx.channel
await ctx.send("Send the thing:")
response = await client.wait_for('message', check=check)
# Two different ways you may want to split your message:
# --- The first way to do it would be to split at a certain keyword, for example 'msg' --- #
role, message = response.content.split('msg:', 1)
# --- The other way would be to split it from the first space --- #
role, message = response.content.split(' ', 1)
# sending the message here
await ctx.send(f"""
{role}
Sponsor: {ctx.author.mention}
Message: {message}
""")
Working Method 1
Working Method 2
References:
wait_for(event, *, check=None, timeout=None)
Upvotes: 1