【问题标题】:How do I make a random reply for my discord bot (in python)如何为我的不和谐机器人随机回复(在 python 中)
【发布时间】:2021-06-02 11:00:34
【问题描述】:

我是制作不和谐机器人的新手,目前有一些基本代码允许用户编写命令,然后我的机器人会回复提供的命令。我只想知道如何使随机回复出现在选项列表中。 再一次,我是制作机器人的新手,对它们了解不多。一些帮助将不胜感激。 (如果我错过了什么也很抱歉,我是这个平台的新手)

import discord
import os
import random

client = discord.Client()

@client.event
async def on_ready():
  print('Current user logged {0.user}'.format(client))
@client.event
async def on_message(message):
  if message.author == client.user:
    return
  if message.content.startswith(']hi'):
    await message.channel.send('hello')

client.run(os.getenv('TOKEN'))

【问题讨论】:

    标签: python discord


    【解决方案1】:

    您可以创建“答案选择”列表。

    列表始终如下所示:["Entry1", "Entry2", "Entry3"]

    您可以在此处输入您想要的答案。 执行命令/事件时,应从此列表中选择一个条目。你可以这样做:

    random = ["entry1", "entry2", "entry3"]
    await message.channel.send(f"{random.choice(random)}") # Choose one random output
    

    我会将整个内容放在一个命令中,因此请使用以下命令:

    import random
    import discord
    from discord.ext import commands
    
    @client.command()
    async def random(ctx):
    random = ["entry1", "entry2", "entry3"]
    await ctx.send(f"{random.choice(random)}")
    

    更多示例请查看docs

    【讨论】:

    • 我创建了一个新文件来测试您提供的代码,它给了我一个错误提示“Traceback (last recent call last): File "main.py", line 9, in @ client.command() AttributeError: 'Client' 对象没有属性 'command' "
    • 你导入from discord.ext import commands了吗?
    • 试试client = commands.Bot(command_prefix="YourPrefix")
    • 当我尝试将它放入我的主代码时,它给了我 12 个不同的错误,即使它自己运行得非常好。
    • 我认为这是因为你如何定义client。如果我是对的,使用当前代码你暂时不能使用client.command
    猜你喜欢
    • 2021-03-29
    • 2021-03-11
    • 2019-06-08
    • 2021-10-30
    • 2021-05-27
    • 2021-07-15
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    相关资源
    最近更新 更多