繁体   English   中英

我如何让我的不和谐机器人说出一个人的名字,然后在他们猜出以聊天形式键入的变量后说出正确的名字

[英]How can I get my discord bot to mention a persons name then say correct after they guess a variable in the form of typing it in the chat

我没有此功能的任何代码,但我有一个我可以在其中实现此代码的机器人。 我是编程新手。

            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('$hello'):
                    await message.channel.send('Hello!')

            client.run('my token is here')

作为新程序员,我建议将您的问题分解为逻辑步骤,并尝试分别解决它们。 您还应该准确定义所有内容,包括所有假设。

一旦解决了这个问题,攻击单个零件就会容易一些。 这是您要阅读文档的地方。

在您开始任何这方面之前,我建议您先阅读一下编程概念的概述。 特别:

  • 变量:包括整数,字符串,浮点数,日期时间等类型
  • 变量操作:-+,-,*,/
  • 对象
  • 基本对象的操作:len,append
  • 集合:列表和字典
  • 布尔逻辑-if语句和,或
  • 循环执行重复性任务-for语句,while语句
  • 职能

整体系统设计

不和谐的机器人的行为类似于不和谐的用户,因此可以一次连接到多个服务器(行会)和渠道。 我们从API使用的事件会给您一条消息,该消息可能来自任何行会或渠道。

单人版Verison

  1. Bot等待用户键入“!game”。 记录公会,频道和用户的ID
  2. 输出一条消息,提示“选择号介于1-10之间”。
  3. 随机生成1-10之间的变量。 将此存储在一个集合中,您可以使用步骤1中的3个ID进行查找。
  4. 扫描该用户的响应。 这将需要验证以确保它们仅输入数字。 仅当游戏实际在特定的公会/频道中运行时才触发。
  5. 使用if语句比较用户的响应
  6. 如果获胜,则宣布游戏结束,并将其从集合中删除。
  7. 否则,告诉他们再试一次。

这是您需要回答此问题的文档:

https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token

https://discordapp.com/developers/docs/intro

https://discordpy.readthedocs.io/en/latest/api.html#message

Aaaaaa和这是代码。

import discord
import random

client = discord.Client()

players = dict()

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

@client.event
async def on_message(message):
    # Check https://discordpy.readthedocs.io/en/latest/api.html#message
    print("Guild ID: ", message.guild.id)
    print("Channel ID: ", message.channel.id)
    print("User ID: ", message.author.id)

    # Unique set of properties for each game instance
    gameInstance = (message.guild.id, message.channel.id, message.author.id)

    # Bot can't talk to itself
    if message.author == client.user:
        return

    # Detect if user wants to play game
    if message.content == "!game":
        # Is user already playing game?
        if (gameInstance) in players:
            await message.channel.send("You are already playing game friend.")
            return

        # Generate random number
        randomNumber = random.randint(1, 10)

        # Save random number in dictionary against that game instance
        players[gameInstance] = randomNumber

        # Ask user for guess
        await message.channel.send("You want to play game?! Please enter a number between 1 and 10.")
        return

    # We only get here if user not entering "!game"
    if gameInstance in players:
        # Check if message only contains digits then convert it to integer and check guess
        # If match, we delete the game instance.
        if message.content.isdigit():
            guess = int(message.content)
            if guess == players[gameInstance]:
                del players[gameInstance]
                await message.channel.send("You Win!")
            else:
                await message.channel.send("Try, try, try, try, try again.")
        else:
            await message.channel.send("That's not a number.")    


token = "YOUR TOKEN"
client.run(token)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM