0

My intention is that when someone writes a message the bot reads it, and if there are many more messages, the bot waits for them to finish reading and then goes on to the next one and so on.

@client.event
async def on_message(msg):
    channel_mute = discord.utils.get(msg.guild.channels, name='mute')

    if not msg.author.bot: # First I check that it is not a bot that sends the message
        if msg.channel.id != channel_mute.id: # I check if the channel where the bot works and where the message has been written are different
            await msg.channel.send('Bot can only be used in its respective channel.')
        else:
            if msg.author.voice == None: # Check my status on the voice channel
                await msg.channel.send('You are not within a voice channel.')
            else:
                text = ''.join(msg.content)
                text_messages = []
                text_messages.append(text)

                user = msg.author
                try:
                    vc = await user.voice.channel.connect()
                except:
                    vc = msg.guild.voice_client

                for m in text_messages:
                    sound = gTTS(text=f'{user.name} say {m}', lang='en', slow=False)
                    sound.save('tts-audio.mp3')

                    if vc.is_playing(): # This conditional is not complete, but the idea is that it works to wait for it to finish reading to move on to the next message in the list.

                    vc.play(discord.FFmpegPCMAudio(executable=r'C:\MY_PATH\ffmpeg.exe', source='tts-audio.mp3'))

In my process of this bot, I got errors like:

  • When sending one or several messages while the bot is reading, the bot stops to read the new message even though the previous one had not finished reading yet
  • When sending several messages, the bot would read the first one, and when it finished it would read the last one, and if there were more messages in the middle of these, it would not take them every now and then and not read them.
  • When sending multiple messages, the bot interpreted them as together and they overlapped

I tried watching videos and reading other people's code but I can't find the solution. My intention is that when someone writes a message the bot reads it, and if there are many more messages, the bot waits for them to finish reading and then goes on to the next one and so on.

galileo
  • 25
  • 5

0 Answers0