为了账号安全,请及时绑定邮箱和手机立即绑定

Discord.py 文本通道检查

Discord.py 文本通道检查

芜湖不芜 2023-05-09 16:06:29
@client.command(aliases=["logchannel, setlog"])@commands.has_permissions(manage_messages=True)async def log(ctx, *args: discord.TextChannel):    with open("configs/channels.json", "r") as f:        channels = json.load(f)    channel = channels.get(str(ctx.guild.id), ctx.channel.id)    if len(args) == 0:        await ctx.send("Which channel should I set the logs? :thinking:")    elif args[0] != discord.TextChannel:        await ctx.send("That is not a valid channel!")    elif args[0] == discord.TextChannel:        with open("configs/channels.json", "w") as f:            json.dump(channels, f, indent=4)        embed = discord.Embed(title="Log channel set! :white_check_mark:",                              description=f"**{channel}** has been set as logs channel!",                              color=0x2f3136)        await ctx.send(embed=embed)所以我的这部分代码是为使用 JSON 设置日志通道而制作的。json 部分工作正常,因为我在其他几个命令中使用过它。但它看不到是否提供了有效的频道。据我所知,它应该是,discord.TextChannel但事实并非如此。该if len(args) == 0部分有效,但其他部分无效。我怎样才能使这项工作?我应该放什么而不是discord.TextChannel?
查看完整描述

3 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

一种更精确的方法是使用TextChannelConverter它在提供 ID/名称/提及时返回通道,而不仅仅是名称

from discord.ext import commands


async def log(ctx, *, args=None):

    if not args:

        await ctx.send("Please provide the channel to set the logs")

        return

    try:

        channel = await commands.TextChannelConverter().convert(ctx, args)

    except:

        return await ctx.send("Channel Not Found")

    #channel is not a TextChannel object, save its ID or send or whatever you want to do


查看完整回答
反对 回复 2023-05-09
?
GCT1015

TA贡献1827条经验 获得超4个赞

我认为它可以基于此工作。


    @commands.command()

    async def test(self, ctx, channel=None):


        channel2 = self.client.get_channel(id=int(channel))


        if type(channel2) != discord.channel.TextChannel:

            await ctx.send('Please do not enter an audio channel')


        else:

            await ctx.send('Perfect.')


查看完整回答
反对 回复 2023-05-09
?
慕标5832272

TA贡献1966条经验 获得超4个赞

Return 只是结束您可以使用它而不是 if 语句的函数。我不知道使用的是什么 JSON 文件,我希望这会有所帮助


async def log(ctx, *, args=None):

    if not args:

        await ctx.send("Which channel should I set the logs? :thinking:")

        return


    for channel in ctx.guild.channels:

        if channel.name == args:

            await ctx.send('Found your channel')

            # channel is now an object, you can do what you want here

            await channel.send('This is the channel wanted')

            return


    await ctx.send("Can't find your channel")


查看完整回答
反对 回复 2023-05-09
  • 3 回答
  • 0 关注
  • 99 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信