如何转换需要用户输入才能在Discord bot中工作的python代码? [英] how do you convert a python code that requires user input to work in a discord bot?

查看:97
本文介绍了如何转换需要用户输入才能在Discord bot中工作的python代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一段代码,它需要用户多次输入(输入的内容并不总是一样)。与其将代码传递给不和谐的所有人,我不希望将其直接放入不和谐的bot中,以便每个人都可以使用它。在给出代码后,我所有的机器人如何接收用户味精?

So I have a piece of code and it requires user input multiple times (and what is inputed is not alway the same). Instead of passing the code to everyone in my discord I would like to make it directly into a discord bot so everyone can use it. How do I all the bot to take in a user msg after a code is given

下面是我想要的示例:

-。botcalc

-这是不和谐的机器人,输入第一个数字:

-1

-输入第二个数字:

-2

--1 + 2 = 3

-.botcalc
--this is discord bot, enter first number:
-1
--enter second number:
-2
--1+2 = 3

推荐答案

使用< a href = https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html?highlight=wait_for rel = nofollow noreferrer> wait_for

async def botcalc(self, ctx):
        author = ctx.author
        numbers = []

        def check(m):
            return m.author ==  author

        for _ in ('first', 'second'):
            await ctx.send(f"enter {_} number")
            num = ""
            while not num.isdigit():
                num = await client.wait_for('message', check=check)
            numbers.append[int(num)]

        await channel.send(f'{numbers[0]}+{numbers[1]}={sum{numbers)}')

编辑

添加了支票

这篇关于如何转换需要用户输入才能在Discord bot中工作的python代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆