【问题标题】:Discord.py - How to (at)mention a Discord user from Bot Command [duplicate]Discord.py - 如何(在)从 Bot 命令中提及 Discord 用户 [重复]
【发布时间】:2021-07-24 05:07:05
【问题描述】:

真的不是骗子吗?说明:
This question is not looking to (at)mention a user
This question lists member.mention but does not provide a complete example

我的问题
使用 Discord.py(如果标题不够清晰),我试图弄清楚如何在 Discord 用户运行命令时向他们添加 @mention 属性。

示例:
用户输入:$99
机器人输出:@User 你的报价是:Bingpot!

更新:2019 年 10 月 24 日
- @epic-programmer 指出了一个非常严重的复制粘贴错误,我已修复 :)
- 该修复将“成员”设置为命令的参数。我想要的是获取成员显示名称,并在命令输出中使用它(作为@提及)

谷歌答案

@commands.command()
async def mention_ping(self, ctx, member : discord.Member):
    await ctx.send(f"PONG {member}")

我的代码(更新:使用@epic-programmers 部分修复)

import random
import discord

from discord.ext import commands

#----
tokenfile = 'token.txt'
with open(tokenfile) as tf:
    line = tf.readline()
    TOKEN = line.rstrip()
#----

bot = commands.Bot(command_prefix='$')

@bot.command(name='99', help='Responds with a random quote from Brooklyn 99')
async def nine_nine(ctx, member : discord.Member):
    brooklyn_99_quotes = [
        'I\'m the human form of the ???? emoji.',
        'Bingpot!',
        (
            'Cool. Cool cool cool cool cool cool cool, '
            'no doubt no doubt no doubt no doubt.'
        ),
    ]

    response = random.choice(brooklyn_99_quotes)
    await ctx.send("{} your quote is: {}".format(member, response))

bot.run( TOKEN)

我看过的资源

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    member 参数位于错误的位置。尝试将其移至 def 语句。

    @bot.command(name='99', help='Responds with a random quote from Brooklyn 99')
    async def nine_nine(ctx, member : discord.Member):
        brooklyn_99_quotes = [
            'I\'m the human form of the ? emoji.',
            'Bingpot!',
            (
                'Cool. Cool cool cool cool cool cool cool, '
                'no doubt no doubt no doubt no doubt.'
            ),
        ]
    
        response = random.choice(brooklyn_99_quotes)
        await ctx.send("{} your quote is: {}".format(member, response))
    
    bot.run( TOKEN)
    

    编辑:为了提及当前用户调用命令,您可以这样做:

    from typing import Optional
    
    @bot.command(name='99', help='Responds with a random quote from Brooklyn 99')
    async def nine_nine(ctx, member : Optional[discord.Member] = None):
        member = member or ctx.author
        brooklyn_99_quotes = [
            'I\'m the human form of the ? emoji.',
            'Bingpot!',
            (
                'Cool. Cool cool cool cool cool cool cool, '
                'no doubt no doubt no doubt no doubt.'
            ),
        ]
    
        response = random.choice(brooklyn_99_quotes)
        await ctx.send("{} your quote is: {}".format(member, response))
    
    bot.run( TOKEN)
    

    此代码块将检查 ping,如果找不到,则默认为调用该命令的用户。

    【讨论】:

    • 哇....我真的很愚蠢,因为没有抓住 -that- :D 有时第二双眼睛真的很有帮助 ;) 这似乎有效,只是我需要将“member”传递为一个论点。最终目标是提及发起命令的用户。示例:输入:$ 99 输出:“@BanjoFox,您的报价是:[来自列表的报价]”我很幸运地将 author = ctx.message.author 添加到函数中,但这并没有@提及用户:(
    猜你喜欢
    • 2021-02-07
    • 2021-12-20
    • 1970-01-01
    • 2020-08-16
    • 2020-07-27
    • 2023-03-24
    • 2021-10-24
    • 2021-04-16
    • 2021-04-13
    相关资源
    最近更新 更多