当前位置: 首页 > 知识库问答 >
问题:

如何解决不和谐。py没有运行语音命令的异步IO功能?

袁阿苏
2023-03-14

在我的不和中。py bot,我正在尝试创建一个函数,该函数运行bot加入语音频道、播放音频文件然后离开所必需的代码。我正在这样做,所以我不必为我想发出的每个语音命令复制和粘贴相同的代码。但是,我无法运行该函数。

我尝试过玩异步def和等待我的函数函数,但是它似乎不工作。我一无所知,因为当我运行代码时,我没有收到错误。

async def voiceCommand(ctx, message, file, time, cmd):
    channel = ctx.message.author.voice.voice_channel
    if channel == None: # If the user who sent the voice command isn't in a voice channel.
        await client.say('You are not in a voice channel, therefore I cannot run this command.')
        return
    else: # If the user is in a voice channel
        print(executed(cmd, message.author))
        voice = await client.join_voice_channel(channel)
        server = ctx.message.server
        voice_client = client.voice_client_in(server)
        player = voice.create_ffmpeg_player(file) # Uses ffmpeg to create a player, however it makes a pop up when it runs.
        player.start()
        time.sleep(float(time))
        await voice_client.disconnect() # Disconnects WingBot from the voice channel.

@client.command(pass_context = True)
async def bigbruh(ctx):
    voiceCommand(ctx, message, 'bigbruh.mp3', '0.5', 'bigbruh')

这些是我试图用来运行函数的代码片段。

完整源代码如下:https://pastebin.com/bv86jSvk

共有1个答案

锺离伟彦
2023-03-14

您可以使用这几个方法在python中运行异步函数

async def print_id():
    print(bot.user.id)

@bot.event
async def on_ready():
    bot.loop.create_task(print_id()) #this will run the print_id function
    print(bot.user.name)

async def not_me(msg):
    await bot.send_message(msg.message.channel,"You are not me")

async def greet():
    print("Hi")

@bot.command(pass_context=True)
async def me(msg):
    if msg.message.author.id == '123123':
        await bot.say("Hello there") #await is used to run async function inside another async function

    else:
        await not_me(msg)


#To run the async function without it being inside another async function you have to use this method or something similar to it
import asyncio

# asyncio.get_event_loop().run_until_complete(the_function_name())
#in this case it's `greet`
asyncio.get_event_loop().run_until_complete(greet())
 类似资料:
  • 我已经创建了一个bot,它现在在我的discord服务器中,使用下面的代码。 我的问题是,一旦我在与bot不和谐的聊天中,我如何调用命令让bot运行代码,为用户列表收集csv?我不确定如何调用机器人,一旦它在聊天/服务器中获得列表。

  • 我试图在重写discord.py不和谐的自我机器人,它给了我这个错误: 这是我的代码:

  • 我开始写一个Discord机器人,但我已经设法遇到了一个问题。我只是写了他写的东西,做了一些小改动,不会对程序产生太大影响。我有两个类,主类只获取bot的令牌,然后使用 下面是MyBot.cs: 它可以连接,机器人也可以在线。这是我的控制台中的输出: 当我现在打字的时候。一般来说,什么都不会发生。控制台中没有,一般情况下也没有。我已经看过了,但它并没有解决我的问题 编辑:我知道我应该使用Comma

  • 问题内容: 我正在尝试执行我的PHP代码,该代码通过mysqli调用两个MySQL查询,并得到错误“命令不同步;您现在不能运行此命令”。 这是我正在使用的代码 我曾尝试阅读此书,但不确定该怎么做。我已经阅读了有关存储结果和免费结果的信息,但是使用它们时并没有什么区别。我不确定此错误的确切原因,并想知道其原因以及如何解决。 按照我的调试语句,由于我的sql语法near附近,甚至没有输入countQu

  • 我正在为我朋友组的游戏服务器开发一个discord机器人。我想添加一个命令,使语音频道中的每个人都静音。我想这是可以工作,但它返回时不是一个函数,导致机器人崩溃。这是的工作原理是,它将服务器静音发送消息的成员,但显然不是我想要的整个频道。我对不和谐是全新的。js和文档有点混乱。谢谢你的时间!

  • 这是如此令人沮丧...我试图安装顺序node.js.我成功地在本地安装了它,但我不能全局安装它(我得到权限拒绝错误: 实际上,我并不希望它是全局安装的,但当它在本地安装时,应该配置并初始化sequelize模块(通过键入sequelize init:models 所以我做了功课,发现命令未找到错误可以解决与全局安装(-bash:续集:命令未找到)和修复错误在启用全局安装我改变了我的用户访问(错误: