程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Discord.py 游戏机器人不执行命令大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Discord.py 游戏机器人不执行命令?

开发过程中遇到Discord.py 游戏机器人不执行命令的问题如何解决?下面主要结合日常开发的经验,给出你关于Discord.py 游戏机器人不执行命令的解决方法建议,希望对你解决Discord.py 游戏机器人不执行命令有所启发或帮助;

我是 Python 的初学者,我正在尝试编写一个不和谐的游戏机器人,您必须在其中选择路径。

我希望用户首先运行 !adv 命令,然后可以按“c”继续,但我的代码无法这样做。

我没有收到任何错误,但在运行 !adv 命令后,我无法运行“c”命令来进一步执行某些操作。


clIEnt = discord.ClIEnt()



@clIEnt.event
async def on_ready():
    general_chAnnel = clIEnt.get_chAnnel(864471185907908608)
    await general_chAnnel.send('hey!')



@clIEnt.event
async def on_message(messagE):
    count = 0
    msg = message.content.lower()
    general_chAnnel = clIEnt.get_chAnnel(864471185907908608)
    if count == 0:
        if msg == '!adv':
            await general_chAnnel.send('Hello '+ message.author.name +'. \nWelcome to the Lands of The Supreme Lord,The Godfather.\nyou woke up in the marketplace with crowd bustling everywhere.\nYou have many questions.\nwhat led you to this place and whats your desTiny?\nYou are set to write your own path here.\nPress c to conTinue.')
        count += 1
    
async def on_message(messagE):
    msg = message.content.lower()
    
    if count == 1:
        if msg == 'c':
            await general_chAnnel.send('\nYou see a vase shop and an weaponsmith ahead.\nDecIDe who you want to talk.\nType v or w')
            

请附上解决方案的代码,因为代码很容易理解。

提前致谢

解决方法

您实际上不能拥有多个 on_message(尽管您没有将第二个注册为事件)。要等待用户响应,您应该使用 client.wait_for:

@client.event
async def on_message(messagE):
    def check(m):  # simple check to wait for the correct user
        return m.author == message.author and m.chAnnel == message.chAnnel

    count = 0
    msg = message.content.lower()
    general_chAnnel = client.get_chAnnel(864471185907908608)

    if count == 0:
        if msg == '!adv':
            await general_chAnnel.send('Hello '+ message.author.name +'. \nWelcome to the Lands of The Supreme Lord,The Godfather.\nyou woke up in the marketplace with crowd bustling everywhere.\nYou have many questions.\nwhat led you to this place and whats your desTiny?\nYou are set to write your own path here.\nPress c to conTinue.')

            # WaiTing for the response
            resp = await client.wait_for("message",check=check)
            if resp.content.lower() == "c":
                # conTinue the story           
                await general_chAnnel.send('\nYou see a vase shop and an weaponsmith ahead.\nDecide who you want to talk.\nType v or w')

                resp2 = await client.wait_for("message",check=check)
                if resp2.content.lower() in ["v","w"]:
                    # conTinue the story
                    ...

        count += 1

您可能想使用一些 return 语句来避免缩进如此深。

大佬总结

以上是大佬教程为你收集整理的Discord.py 游戏机器人不执行命令全部内容,希望文章能够帮你解决Discord.py 游戏机器人不执行命令所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。