Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

python - How to make a working introduction feature in my discord.py bot?

I'm trying to add an intro feature to my discord.py bot. This is how I want it to work:

Someone sends ".intro", and the bot starts asking a bunch of personal questions to the user. Once they answer all the questions, the bot makes an embed storing all those answers in it, and sends the embed to a channel called "intro". And then, when someone wants to find the intro of a particular user, they do ".whois @user", which tells the bot to find the intro created by the mentioned user, so the bot can send it back. When someone has already made their introduction and wants to edit it, they do ".intro" once again, and enter the required answers, and the bot edits their intro in the "intro" channel.

I'm very new to coding, and I have no idea how to implement this. I have wrote the code for making the required intro embed, but I don't have a clue on how to store different people's answers in different embeds. Any help would be highly appreciated! Thank you.

This is my intro cog:

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))
question from:https://stackoverflow.com/questions/65907528/how-to-make-a-working-introduction-feature-in-my-discord-py-bot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Messageable.send() returns the discord.Message instance of the message it sent, so you can use that to store the id of the message in a database, together with the id of the user that the message belongs to.

That way, you can easily check if a person is in there (-> check if the user's id is in the database), get their message (using fetch_message(), finding the one that belongs to the given user's id in your database), and allow the user to edit their message using Message.edit() after fetching it.

When someone uses .whois, you can just send them the jump_url to take them to the corresponding message.

This should be enough of an explanation to get you going. I'm not just gonna give you the code to do it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...