Home > other >  Replying to a message through a discord bot
Replying to a message through a discord bot

Time:11-27

I want when I say

$script

to output just any message for now in this below example 'pong'.

I have my token in a separate .env file so I don't need to worry about it

import discord
import os
from discord.ext import commands

intents = discord.Intents.all()



client = discord.Client(intents=discord.Intents.default())

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


@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@bot.command(name="script")
async def getscript(ctx):
    await ctx.channel.send("pong")

    
client.run(os.getenv('TOKEN'))

this is the code below, but upon saying $script, nothing happens the bot is online and it works if I just send a message, but I'm not sure why this isn't working can someone help me out here? I have look at the "do any of these posts answer your question?" section and nothing helps.

CodePudding user response:

Well yes it doesn't work because you're not running it. You have both a Client and a Bot. Your Bot has the $script command, but you're running the Client...

# You have both a Client and a Bot
client = discord.Client(...)
bot = commands.Bot(...)

# The command is registered to the Bot
@bot.command(...)

# You're running the Client 
client.run()

Either use a Bot or a Client, but you can't use both. Get rid of your Client and use the Bot for everything instead. commands.Bot is a subclass of discord.Client so it can do everything a Client can do.

There's 0 reason to have both.

  • Related