Command that's only available to the owner of the bot, but bot will send the message to every guild it is in

Question:

Make a command that’s only available for the owner of the bot to use, but the bot will send the message said by the user to every guild.bot is in. I am also doing python for this.

My Code:

import discord
import os
from discord.ext import commands
from keep_alive import keep_alive
intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)
prefix = os.environ['PREFIX']


@client.event
async def on_ready():
    print(f'Logged in as {client.user}')
    await client.change_presence(activity=discord. Activity(type=discord.ActivityType.watching, name='Nothing <3'))

@client.event
async def owner(x):
  if x.author.id == '751535313167581265':
      return
  if x.content.startswith(owner_prefix + 'x'):
      await x.channel.send(f'Test completed')

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

I am trying to do this owner command, but it fails and says, something close to the object is not in the argument. Please correct my code.

Asked By: Dark

||

Answers:

There are several things that aren’t quite right in your code.

  1. You’re using client.event instead of client.command. While it is possible to use client.event it would make your life easier if you used client.command.
  2. Variable x. This is not a good variable. In an on_message event, this would be message. In a command, this would be discord.ext.commands.Context aka ctx.
  3. The attribute id of author is an integer. You are checking for a string.
  4. Looking at your code, I don’t think owner_prefix is defined (correct me if I’m wrong).

Before you start coding such things, you should first learn the basics. This could be through the documentation or online tutorials.

@client.command() # 1.
async def owner(ctx, arg=None): # 2.
  if ctx.author.id == 751535313167581265: # 3.
      return
  else:
      await ctx.channel.send(arg) #The usage of f-string was also useless here

I apologise for my past carelessness, in order to use @client.command(), you have to use discord.ext.commands.Bot. discord.Client doesn’t have a method command, hence the error. Since Bot is a subclass of Client, you can use all of the Client functionality with the Bot instance.

from discord.ext import commands
#rest of imports
intents = discord.Intents.default()
intents.message_content = True

prefix = os.environ['PREFIX']
client = commands.bot(prefix, intents=intents)

A variable name is a just a variable name. To not confuse you or anyone else who reads your code, you could change client to bot and likewise @client.command() would be @bot.command().

Answered By: Raymus

Raymus thanks for the help, I think I got it. I will ask for more tips later this week, apparently I can’t post any questions.

I used it but it said “client” had no attribute to command.

I stopped trying to use the command, I might do it more later this week, pr next week.

Thanks again , I appreciate the help

Answered By: Dark
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.