discord.py 宣布命令

discord.py announce command

我正在尝试让我的 announce 命令将响应发送到公告频道,但我不想使用频道 ID,因为我正在为多个服务器构建机器人并且更愿意保持简单。

不幸的是,它不起作用..这是我当前的代码。

import discord
from discord.ext import commands

class Announce(commands.Cog):
    def __init__(self, client):
        self.client = client


    @commands.command(name="Announce", aliases=["A", "a", "announce", "Ann", "ann", "ANN"])
    @commands.has_permissions(manage_messages=True)
    async def _announce(self, ctx, *, message):
        """Sends an announcement via the bot
        Alt     : a, A, ANN, Ann, ann, announce
        Usage   : [ann]ounce <message>"""
        for channel in ctx.guild.channels:
            if str(channel) == "announcements":
                await ctx.message.delete()
                embed = discord.Embed(color=discord.Color.dark_gold(), timestamp=ctx.message.created_at)
                embed.set_author(name="Announcement", icon_url=self.client.user.avatar_url)
                embed.add_field(name=f"Sent by {ctx.message.author}", value=str(message), inline=False)
                embed.set_thumbnail(url=self.client.user.avatar_url)
                embed.set_footer(text=self.client.user.name, icon_url=self.client.user.avatar_url)
                await ctx.message.add_reaction(emoji="✅")
                await ctx.channel.send(embed=embed)


    @_announce.error
    async def _announce_error(self, ctx, error):
        if isinstance(error, commands.MissingRequiredArgument):
            embed = discord.Embed(color=discord.Color.dark_red(), timestamp=ctx.message.created_at)
            embed.set_author(name="Error", icon_url=self.client.user.avatar_url)
            embed.add_field(name="Command Failed", value="Please pass in all required arguments.")
            embed.set_thumbnail(url=self.client.user.avatar_url)
            embed.set_footer(text=self.client.user.name, icon_url=self.client.user.avatar_url)
            await ctx.message.add_reaction(emoji="⚠")
            await ctx.message.author.send(embed=embed)

        elif isinstance(error, commands.MissingPermissions):
            embed = discord.Embed(color=discord.Color.dark_red, timestamp=ctx.message.created_at)
            embed.set_author(name="Error", icon_url=self.client.user.avatar_url)
            embed.add_field(name="Access Denied", value="You do not have permission to use this command")
            embed.set_thumbnail(url=self.client.user.avatar_url)
            embed.set_footer(text=self.client.user.name, icon_url=self.client.user.avatar_url)
            await ctx.message.add_reaction(emoji="⛔")
            await ctx.message.author.send(embed=embed)

def setup(client):
    client.add_cog(Announce(client))

我试过让它像这样工作

@commands.command(name="Announce", aliases=["A", "a", "announce", "Ann", "ann", "ANN"])
    @commands.has_permissions(manage_messages=True)
    async def _announce(self, ctx, *, message, member):
        """Sends an announcement via the bot
        Alt     : a, A, ANN, Ann, ann, announce
        Usage   : [ann]ounce <message>"""
        for channel in ctx.member.guild.channels:
            if str(channel) == "announcements":
                await ctx.message.delete()
                embed = discord.Embed(color=discord.Color.dark_gold(), timestamp=ctx.message.created_at)
                embed.set_author(name="Announcement", icon_url=self.client.user.avatar_url)
                embed.add_field(name=f"Sent by {ctx.message.author}", value=str(message), inline=False)
                embed.set_thumbnail(url=self.client.user.avatar_url)
                embed.set_footer(text=self.client.user.name, icon_url=self.client.user.avatar_url)
                await ctx.message.add_reaction(emoji="✅")
                await ctx.channel.send(embed=embed)


    @_announce.error
    async def _announce_error(self, ctx, error):
        if isinstance(error, commands.MissingRequiredArgument):
            embed = discord.Embed(color=discord.Color.dark_red(), timestamp=ctx.message.created_at)
            embed.set_author(name="Error", icon_url=self.client.user.avatar_url)
            embed.add_field(name="Command Failed", value="Please pass in all required arguments.")
            embed.set_thumbnail(url=self.client.user.avatar_url)
            embed.set_footer(text=self.client.user.name, icon_url=self.client.user.avatar_url)
            await ctx.message.add_reaction(emoji="⚠")
            await ctx.message.author.send(embed=embed)

        elif isinstance(error, commands.MissingPermissions):
            embed = discord.Embed(color=discord.Color.dark_red, timestamp=ctx.message.created_at)
            embed.set_author(name="Error", icon_url=self.client.user.avatar_url)
            embed.add_field(name="Access Denied", value="You do not have permission to use this command")
            embed.set_thumbnail(url=self.client.user.avatar_url)
            embed.set_footer(text=self.client.user.name, icon_url=self.client.user.avatar_url)
            await ctx.message.add_reaction(emoji="⛔")
            await ctx.message.author.send(embed=embed)

然而这也不起作用。任何帮助将非常感激。 我更喜欢使用频道名称而不是频道 ID,这样可以使事情变得更简单并减少我必须输入的代码量。

如果有帮助,我正在使用 discord.py 重写。

我将在您的方法中指出 channel 是一个通道对象,请参阅 here 因此要使您的工作正常,由于 oop,它需要 str(channel.name)。 (面向对象编程)

但是,让我介绍一个竞争者。 discord.utils.get,参见here,可以用来轻松完成你想要的。

@commands.command()
async def announce(self, ctx):
    # Find a channel from the guilds `text channels` (Rather then voice channels)
    # with the name announcements
    channel = discord.utils.get(ctx.guild.text_channels, name="announcements")
    if channel: # If a channel exists with the name

                embed = discord.Embed(color=discord.Color.dark_gold(), timestamp=ctx.message.created_at)
                embed.set_author(name="Announcement", icon_url=self.client.user.avatar_url)
                embed.add_field(name=f"Sent by {ctx.message.author}", value=str(message), inline=False)
                embed.set_thumbnail(url=self.client.user.avatar_url)
                embed.set_footer(text=self.client.user.name, icon_url=self.client.user.avatar_url)
                await ctx.message.add_reaction(emoji="✅")
                await channel.send(embed=embed)

我们想发送到我们的 channel 对象而不是我们发送命令的同一个频道,还删除了我们删除正在调用的命令,否则添加表情符号会出错。希望这对您有所帮助!