【问题标题】:How to make a working introduction feature in my discord.py bot?如何在我的 discord.py 机器人中创建有效的介绍功能?
【发布时间】:2021-04-30 14:26:41
【问题描述】:

我正在尝试向我的 discord.py 机器人添加介绍功能。这就是我希望它的工作方式:

有人发送“.intro”,机器人开始向用户提出一堆个人问题。一旦他们回答了所有问题,机器人就会制作一个嵌入文件,将所有这些答案存储在其中,并将嵌入文件发送到一个名为“intro”的频道。然后,当有人想要查找特定用户的介绍时,他们会执行“.whois @user”,这会告诉机器人找到由提到的用户创建的介绍,因此机器人可以将其发回。当有人已经做了他们的介绍并想要编辑它时,他们再次执行“.intro”,并输入所需的答案,机器人会在“intro”频道中编辑他们的介绍。

我对编码很陌生,我不知道如何实现它。我已经编写了用于制作所需介绍嵌入的代码,但我不知道如何将不同人的答案存储在不同的嵌入中。任何帮助将不胜感激!谢谢。

这是我的介绍齿轮:

import discord
from discord.ext import commands
import asyncio


class IntroSystem(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command()
    async def intro(self, ctx):
        global name, location, age, gender, hobbies
        await ctx.send("What's your name?")

        try:
            name = await self.client.wait_for(
                "message",
                timeout=20,
                check=lambda message: message.author == ctx.author
                                      and message.channel == ctx.channel
            )
        except asyncio.TimeoutError:
            await ctx.send('Timeout! Restart by saying `.intro`')
            return

        await ctx.send("Where do you live?")

        try:
            location = await self.client.wait_for(
                "message",
                timeout=20,
                check=lambda message: message.author == ctx.author
                                      and message.channel == ctx.channel
            )
        except asyncio.TimeoutError:
            await ctx.send('Timeout! Restart by saying `.intro`')
            return

        await ctx.send("How old are you?")

        try:
            age = await self.client.wait_for(
                "message",
                timeout=20,
                check=lambda message: message.author == ctx.author
                                      and message.channel == ctx.channel
            )
        except asyncio.TimeoutError:
            await ctx.send('Timeout! Restart by saying `.intro`')
            return

        await ctx.send("What's your gender? `Male, Female or Non Binary`")

        try:
            gender = await self.client.wait_for(
                "message",
                timeout=20,
                check=lambda message: message.author == ctx.author
                                      and message.channel == ctx.channel
            )
        except asyncio.TimeoutError:
            await ctx.send('Timeout! Restart by saying `.intro`')
            return

        await ctx.send("What are your hobbies or interests?")

        try:
            hobbies = await self.client.wait_for(
                "message",
                timeout=60,
                check=lambda message: message.author == ctx.author
                                      and message.channel == ctx.channel
            )
        except asyncio.TimeoutError:
            await ctx.send('Timeout! Restart by saying `.intro`')
            return

        embed = discord.Embed(
            title='',
            description='',
            colour=discord.Color.blue()
        )
        embed.set_thumbnail(url=ctx.message.author.avatar_url)
        embed.set_author(name=ctx.message.author, url=ctx.message.author.avatar_url)
        embed.add_field(name="Name", value=name.content, inline=True)
        embed.add_field(name="Location", value=location.content, inline=True)
        embed.add_field(name="Age", value=age.content, inline=True)
        embed.add_field(name="Gender", value=gender.content, inline=False)
        embed.add_field(name="Hobbies", value=hobbies.content, inline=False)

        await ctx.send(embed=embed)


def setup(client):
    client.add_cog(IntroSystem(client))

【问题讨论】:

    标签: python discord bots discord.py discord.py-rewrite


    【解决方案1】:

    Messageable.send() 返回它发送的消息的discord.Message 实例,因此您可以使用它将id of the message 与消息所属的id of the user 一起存储在database 中。

    这样,您可以轻松检查是否有人在那里(-> 检查用户的id 是否在数据库中),获取他们的消息(使用fetch_message(),找到属于给定用户的id),并允许用户在获取消息后使用Message.edit() 编辑他们的消息。

    当有人使用.whois 时,您只需向他们发送jump_url 即可将他们带到相应的消息中。

    这应该足以让您继续前进。我不只是会给你代码来做这件事。

    【讨论】:

    • 很抱歉,如何创建数据库?介绍频道可以是数据库,还是我必须学习 MySQL 或其他东西才能制作一个?抱歉问了太多问题。
    • 通道不是数据库...是的,我的意思是 SQL 数据库。要么使用 SQL,要么使用 JSON 文件,但请注意 JSON 并非真正用于此目的。您可以在互联网上找到这两个基本示例。
    【解决方案2】:

    我也很新,但我认为您可以使用用户名创建一个文件并将信息放在那里,然后每次有人执行“.whois @user”时,它都会在您的文件中搜索用户并说信息。 你可以试试,我觉得可行

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多