0

Whatever I do my bot simply refuses to read any messages. Messages do not appear in the run menu.

import discord
from discord.ext import commands
import youtube_dl 

intents = discord.Intents.default()
intents.typing = False
intents.messages = True
intents.presences = True  # Enable the Presence Intent
intents.voice_states = True

bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')
    print('------')

    print('Guilds (servers):')
    for guild in bot.guilds:
        print(f'- {guild.name} (id: {guild.id})')

@bot.command()
async def join(ctx):
    channel = ctx.author.voice.channel
    await channel.connect()

@bot.command()
async def leave(ctx):
    voice_client = ctx.guild.voice_client
    if voice_client:
        await voice_client.disconnect()

@bot.command()
async def play(ctx, *, query):
    voice_client = ctx.guild.voice_client
    if not voice_client:
        channel = ctx.author.voice.channel
        voice_client = await channel.connect()

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(query, download=False)
        url = info['formats'][0]['url']

    voice_client.stop()
    voice_client.play(discord.FFmpegPCMAudio(url, before_options="-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"))

@bot.command()
async def stop(ctx):
    voice_client = ctx.guild.voice_client
    if voice_client and voice_client.is_playing():
        voice_client.stop()

bot.run('MY TOKEN SHOULD BE HERE')

Messages should appear here but they are not enter image description here

I gave my bot all the permissions in the Discord applications there are no errors in the console or while debugging

1 Answer 1

0

hope all is well.

In my personal experience, async print will not execute visibly if:

  1. async function/method is not awaited
  2. asyncio.run() is top level and your python version is greater than 3.7
# your code goes here


def main() -> None:
    bot.run()

if __name__ == '__main__':
    main()

if this doesnt work

I would try:

  1. edit the bot.command to apply await
  2. write another decorator that auto awaits.
  3. create a queue.Queue that loops over a queue with strings to print to the terminal.
import queue 
import multiprocessing as mp
import atexit


TerminateFlag: str = "<Bye>"
TerminalQueue = mp.Queue()

def end_checker():
   global TerminalQueue, TerminateFlag
   
   try:    
       TerminalQueue.put(TerminateFlag)
   except:
       pass

def thread_queue_checker(queue: Queue.queue) -> None:
   
   global TerminateFlag

   atexit.register(end_checker)

   while True:

       if queue.empty() == False:

           if item == TerminateFlag:
               break
           
           item = queue.get()

           print(item)


if __name__ == '__main__':
   mp.frozen_support()

   check_process = mp.Process(target=thread_queue_checker, args=(TerminalQueue, ))
   check_process.run() 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.