asaf meizner
asaf meizner

Reputation: 39

Discord.py how to mention someone that's mentioned in the command

Iv created these code here that when someone rights "send @user to court" it will say "sending @user to court" but it doesn't work here's the code:

import discord
from discord.ext import commands
import ctx
import re

class MyClient(discord.Client):

    async def on_ready(self):
       print('Logged on as', self.user)

    async def on_message(self, message):
        if message.author == self.user:
            return

        messageContent = message.content
        if len(messageContent) > 0:
            if re.search("^send.*court$", messageContent):
                user_id = message.mentions[0].id
                user = self.get_user(user_id)
                await message.channel.send("sending", user, "to court!")

client = MyClient()
client.run('My secret token')

Upvotes: 3

Views: 2479

Answers (2)

user14505884
user14505884

Reputation:

You are sending a user object. To send a mention you have to do "<@{userid}" to mention

Here is what you need to do

await message.channel.send(
                    f"sending <@{user_id}> to court!"
                )

Upvotes: 3

loloToster
loloToster

Reputation: 1413

Firstival you cant use await message.channel.send("sending", user, "to court!") because you wanted to pass 3 arguments when it takes 1 you just have to format the string. Secondly you don't have to get id of the user and then get user object with get_user(id) because you already have this user object in message.mentions[0] here is the final sample:

import discord
from discord.ext import commands
import re

class MyClient(discord.Client):
    async def on_ready(self):
        print("Logged on as", self.user)

    async def on_message(self, message):
        if message.author == self.user:
            return

        messageContent = message.content
        if len(messageContent) > 0:
            if re.search("^send.*court$", messageContent):
                await message.channel.send(
                    f"sending {message.mentions[0].name} to court!"
                )


client = MyClient()
client.run("secret token")

Upvotes: 0

Related Questions