我正在尝试获取我的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重写。
答案 0 :(得分:0)
我将在您的方法中指出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
对象,而不是发送命令的相同通道,还删除了我们删除正在调用的命令,否则添加表情符号将会出错。希望这会有所帮助!