【问题标题】:discord.py trying to set up a moderation log channeldiscord.py 尝试设置审核日志通道
【发布时间】:2021-07-26 03:24:43
【问题描述】:

所以我正在尝试建立一个日志通道,每当有人被踢时,它都会在其中发布一条消息。目前我将它存储在一个 json 文件中,但我无法让它工作。有人有代码吗?这是我的:

with open('log_channels.json', 'r') as f:
      log_channels = json.load(f)

@client.command()
@commands.has_permissions(administrator = True)
async def modlog(ctx, channel = None):
  log_channels[str(ctx.guild.id)] = channel.split()
  with open('log_channels.json', 'w') as f:
    json.dump(log_channels, f)
  await ctx.send(f'Mod log set to {channel}')

@client.command()
@commands.has_permissions(kick_members = True)
async def kick(ctx, member : discord.Member, *, reason = None):
 await member.kick(reason = reason)
 await ctx.send(embed=discord.Embed(title=f'{member} has been kicked.', color= 0xFF8633))
 guild = ctx.guild
 log_channels.get((guild.id) ('0'))
 print (log_channels)
 channel = client.get_channel(log_channels)
 await channel.send(f"test")

【问题讨论】:

  • 你不能让它工作是什么意思,会发生什么?有什么错误吗?
  • 是的,discord.ext.commands.errors.CommandInvokeError:命令引发异常:TypeError:'int'对象不可调用
  • 它打印的是哪一行,您应该在问题本身中提供整个回溯。顺便说一句,阅读stackoverflow.com/help/how-to-ask

标签: python python-3.x discord discord.py


【解决方案1】:

您的错误来自这一行:

log_channels.get((guild.id) ('0'))

更准确地说,来自(guild.id) ('0'),相当于123456789012345678('0'),是不可能的。

你可能打算这样做:

@client.command()
@commands.has_permissions(kick_members = True)
async def kick(ctx, member : discord.Member, *, reason = None):
    await member.kick(reason = reason)
    await ctx.send(embed=discord.Embed(title=f'{member} has been kicked.', color= 0xFF8633))

    channel_id = log_channels.get(str(ctx.guild.id), '0')
    channel = client.get_channel(channel_id)

    await channel.send(f"test")

注意:json dict 键必须是字符串,所以你需要得到str(ctx.guild.id)
此外,如果log_channel 等于'0'channel 将是Nonechannel.send() 将引发错误。

我建议这样做:

@client.command()
@commands.has_permissions(kick_members = True)
async def kick(ctx, member : discord.Member, *, reason = None):
    await member.kick(reason = reason)
    await ctx.send(embed=discord.Embed(title=f'{member} has been kicked.', color= 0xFF8633))

    if str(ctx.guild.id) in logs_channels:
        channel = client.get_channel(log_channels[str(ctx.guild.id)])
        await channel.send(f"test")
    else:
        # Not mandatory, you can remove the else statement if you want
        print('No log channel')

【讨论】:

  • 它出现了错误:discord.ext.commands.errors.CommandInvokeError:命令引发异常:TypeError:不可散列的类型:'list'。我存储 json 数据的方式会有问题吗?
  • 能否在您的问题中包含您的 json 文件结构?
  • 为什么log_channels[str(ctx.guild.id)] 会是一个列表?你有多个日志通道吗?
  • 你知道如何从数值中删除符号吗?
猜你喜欢
  • 2021-10-10
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 2019-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多