【问题标题】:Getting message content with Discord.py使用 Discord.py 获取消息内容
【发布时间】:2020-06-19 20:32:23
【问题描述】:

我想要一个像$status idle 这样的命令,它会执行以下操作

variblething = 'I am idle'
activity = discord.Game(name=variblething)
await client.change_presence(status=discord.Status.idle, activity=activity)

然后他们可以做$message write what you want the status to be 那么它将改变活动消息的内容。我希望这一切都有意义。我当前的代码如下

import discord
client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('$helloThere'):
        await message.channel.send('General Kenobi')

【问题讨论】:

    标签: python python-3.x discord.py discord.py-rewrite


    【解决方案1】:

    首先,我强烈建议您使用前缀和命令装饰器/函数。

    这意味着您的on_message() 函数不会长达一英里。要设置机器人的命令前缀,您可以:

    from discord.ext import commands
    bot = commands.Bot(command_prefix ='$')
    

    这将允许您发出如下命令:

    @bot.command()
    async def changeStatus(ctx, *, status):
    
        if status == 'online':
            new_status = discord.Status.online
        elif status == 'offline':
            new_status = discord.Status.offline
        elif status == 'idle':
            new_status = discord.Status.idle
        elif status == 'dnd':
            new_status = discord.Status.dnd
        else:
            return
    
        await bot.change_presence(status=new_status, activity=discord.Game(f'I am {status}'))
    

    【讨论】:

    • 我从未使用过@bot.command 那么命令是什么?会是$status online activity吗?对不起,如果这是一个愚蠢的问题。
    • 命令的文档可以在这里找到:Docs。对于我发出的命令,不和谐的命令是$changeStatus online。如果您想要活动,则需要更改函数参数。
    • 当我执行$changeStatus idle 时,它什么也没做,控制台没有任何错误,也没有改变状态,也许我做错了什么?我不确定。
    • 代码对我有用。您需要确保包含前缀并将任何变量(例如 bot)编辑为您所拥有的任何变量。
    • 似乎改变了活动,但并没有改变状态。
    猜你喜欢
    • 2021-01-28
    • 2021-06-17
    • 2022-08-15
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 2019-01-12
    • 2019-04-21
    相关资源
    最近更新 更多