Gr33nBean
Gr33nBean

Reputation: 11

Getting a discord bot to mention other users

I am working on a bot to do some simple commands for my discord server and I haven't been able to figure out how to get the bot to mention people who aren't the author.

if message.content.startswith("+prank"):
        user = client.get_user_info(id)
        await client.send_message(message.channel, user.mention + 'mention')

When I try to run the command i come up with the error message:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:/Users/user/Desktop/Murk-Bot 2.0.py", line 130, in on_message
    await client.send_message(message.channel, user.mention + 'mention')
AttributeError: 'generator' object has no attribute 'mention'

This happens if I use the command with a mention before, after, and not at all. If it gives some more context here are the imports I am using

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random

Upvotes: 1

Views: 6942

Answers (2)

Patrick Haugh
Patrick Haugh

Reputation: 61042

It looks like you're trying to implement commands without actually using commands. Don't put everything in the on_message event. If you're not sure how to use the discord.ext.commands module, you can look at the documentation for the experimental rewrite branch.

import discord
from discord.ext.commands import Bot

bot = Bot(command_prefix='+')

@bot.command()
async def prank(target: discord.Member):
    await bot.say(target.mention + ' mention')

bot.run('token')

You would use this command with +prank Johnny. The bot would then respond in the same channel @Johnny mention

Upvotes: 0

Benjin
Benjin

Reputation: 3495

The specific error you are getting is caused by not awaiting a coroutine. client.get_user_info is a coroutine and must use await.

If you want "+prank" to work by mentioning by username, you can find a member object by using server.get_member_named.

Example code provided below. This will check the server the command was called from for the specified username and return the member object.

if message.content.startswith("+prank"):
    username = message.content[7:]
    member_object = message.server.get_member_named(username)
    await client.send_message(message.channel, member_object.mention + 'mention')

Upvotes: 1

Related Questions