0

I asked ChatGPT to generate some code for a Discord bot that uses commands prefixed with /. It came up with:

import discord
import asyncio
import aiohttp
from discord import Object
from discord.ext import commands 
bot = commands.Bot(command_prefix='/', command_attrs={'hidden': False})
intents = discord.Intents.all()
intents.members = True


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


@bot.command(name='radio')
async def radio(ctx, url: str):
    if not url:
        await ctx.send('Please specify a URL for the radio stream')
        return
    voice_channel = ctx.author.voice.channel
    if voice_channel is None:
        await ctx.send('You are not in a voice channel')
        return

    vc = ctx.guild.voice_client
    if vc is not None:
        await vc.disconnect()

    try:
        vc = await voice_channel.connect()
    except discord.ClientException as e:
        await ctx.send(f'Error connecting to voice channel: {e}')
        return

    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as resp:
                if resp.status != 200:
                    await ctx.send(f'Could not connect to the radio stream ({resp.status})')
                    return

                data = await resp.content.read()

                audio_source = discord.FFmpegPCMAudio(data)
                player = vc.play(discord.PCMVolumeTransformer(audio_source))
                await player.start()
    except Exception as e:
        await ctx.send(f'Error playing radio stream: {e}')
        return

    await ctx.send('Playing radio stream')

@bot.command(name='leave')
async def leave(ctx):
    vc = ctx.guild.voice_client
    if vc is not None:
        await vc.disconnect()
    await ctx.send('Left voice channel')

@bot.command(name='join')
async def join(ctx):
    voice_channel = ctx.author.voice.channel
    if voice_channel is None:
        await ctx.send('You are not in a voice channel')
        return

    vc = ctx.guild.voice_client
    if vc is not None:
        await vc.disconnect()
    try:
        vc = await voice_channel.connect()
    except discord.ClientException as e:
        await ctx.send(f'Error connecting to voice channel: {e}')
        return
    await ctx.send('Connected to voice channel')




bot.run('bot token here (yes my token was here)')

It does not work. How can I fix the code? ChatGPT was unable to fix it with further queries, and I couldn't find a solution on GitHub or Stack Overflow either.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Does this answer your question? [How do I get the discord.py intents to work?](https://stackoverflow.com/questions/64831017/how-do-i-get-the-discord-py-intents-to-work) – ESloman Feb 03 '23 at 06:48
  • 1
    ChatGPT is putting the intents declaration after creating the bot object. It's gotta be done before. – ESloman Feb 03 '23 at 06:48
  • Might be worth going through the 'quickstart' tutorial and flicking through some of the documentation rather than expecting an AI to write your code for you. – ESloman Feb 03 '23 at 06:53
  • Also you need to sync your commands to the discord server. Normally this is done in the on_ready event, you can find an example [here](https://stackoverflow.com/a/71169236/13976030) – Doluk Feb 03 '23 at 07:08
  • Welcome to Stack Overflow. Please read [ask] and note well that this is **not a discussion forum**. Please try to write clearly and formally, and do not talk about yourself in the question - talk **only** about what is **necessary to understand the problem**. Make sure to **ask a specific question** clearly, and to **describe a specific problem**. We don't offer a debugging service; it is your responsibility to be able to **diagnose** what the code is doing (see https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for hints). – Karl Knechtel Feb 03 '23 at 09:43
  • Also, please make sure you understand that **ChatGPT cannot possibly solve your programming problems, except by coincidence**. (If it could, the productivity of major tech companies would have skyrocketed overnight.) It only writes prose (treating programming languages as if they were another human language) based on a (very advanced) predictive model of what words go in what order, when discussing a certain topic. It does not, and cannot, reason about what it is writing. – Karl Knechtel Feb 03 '23 at 09:45

1 Answers1

0

solution was here https://stackoverflow.com/a/71169236/13976030

thanks Doluk for the help

For anyone reading this, it turns out I'm a super python noob and was also at times using discord.py and pycord lingo interchangeably thus throwing me errors. I'll be adding the bot to GitHub or something so anyone can have a base and learn off of it (pycord btw).

This bot is a result bet and a curiosity of mine I just want to listen to radio without bloat that other radio bots have and I want it to be able to be easily self-hosted by any newbie like me using python on windows terminal. Additionally, if you're interested in helping with anything or want to use the code do so by all means here's the updated code

import asyncio
import aiohttp
from discord.ext import commands

intents = discord.Intents.all()
intents.members = True

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

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

@bot.slash_command(name='radio')
async def radio(ctx, url: str):
    if not url:
        await ctx.respond('Please specify a URL for the radio stream')
        return
    voice_channel = ctx.author.voice.channel
    if voice_channel is None:
        await ctx.respond('You are not in a voice channel')
        return

    vc = ctx.guild.voice_client
    if vc is not None:
        await vc.disconnect()

    try:
        vc = await voice_channel.connect()
    except discord.ClientException as e:
        await ctx.respond(f'Error connecting to voice channel: {e}')
        return

    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as resp:
                if resp.status != 200:
                    await ctx.respond(f'Could not connect to the radio stream ({resp.status})')
                    return

                data = await resp.content.read()

                audio_source = discord.FFmpegPCMAudio(data)
                vc.play(discord.PCMVolumeTransformer(audio_source))
    except Exception as e:
        await ctx.respond(f'Error playing radio stream: {e}')
        return

    await ctx.respond('Playing radio stream')

@bot.slash_command(name='leave')
async def leave(ctx):
    vc = ctx.guild.voice_client
    if vc is not None:
        await vc.disconnect()
    await ctx.respond('Left voice channel')

@bot.slash_command(name='join')
async def join(ctx):
    voice_channel = ctx.author.voice.channel
    if voice_channel is None:
        await ctx.respond('You are not in a voice channel')
        return

    vc = ctx.guild.voice_client
    if vc is not None:
        await vc.disconnect()
    try:
        vc = await voice_channel.connect()
    except discord.ClientException as e:
        await ctx.respond(f'Error connecting to voice channel: {e}')
        return
    await ctx.respond('Connected to voice channel')

bot.run('your_bot_token_here')

can't wait to learn more from this project!