使用 json python 存储频道 ID 和公会 ID
Posted
技术标签:
【中文标题】使用 json python 存储频道 ID 和公会 ID【英文标题】:storing channel ids and guild ids with json python 【发布时间】:2021-09-18 23:59:30 【问题描述】:代码:
def get_channels(client, guild):
with open('channels.json','r') as f:
channelss = json.load(f)
return channelss[str(guild.id)]
@client.event
async def on_ready():
print("testing...")
@client.command()
async def setreportchannel(ctx, channels: discord.TextChannel):
with open('channels.json','r') as f:
channel = json.load(f)
channel[str(ctx.guild.id)] = int(channels.id)
with open('channels.json','w') as f:
json.dump(channel,f,indent=4)
await ctx.send(f"set the report channel to channels")
@client.command()
async def report(ctx, member:discord.Member, *, reason):
channel = client.get_channel(get_channels)
await ctx.channel.send(f"member has been reported for: reason")
JSON 文件: “850356901232771082”:850357833538338816
问题: 它没有在存储在 json 文件中的正确通道中发送,它假设在用户使用命令 setreportchannel 设置通道的位置
【问题讨论】:
这对您有帮助吗? Trying to make bot log something when a command is used discord.py 是的,谢谢,多亏了它解决了 【参考方案1】: def get_channels(client, guild):
with open('channels.json','r') as f:
channelss = json.load(f)
return channelss[str(guild.id)]
@client.event
async def on_ready():
print("testing...")
@client.command()
async def setreportchannel(ctx, channels: discord.TextChannel):
with open('channels.json','r') as f:
channelss = json.load(f)
channelss[str(ctx.guild.id)] = int(channels.id)
with open('channels.json','w') as f:
json.dump(channelss,f,indent=4)
await ctx.send(f"set the report channel to channels")
@client.command()
async def report(ctx, member:discord.Member, *, reason):
with open('channels.json', 'r') as f:
channelss = json.load(f)
channel_log = ctx.guild.get_channel(channelss[str(ctx.guild.id)])
await channel_log.send(f"member has been reported for: reason")
所以有人找到了它并帮助了我(不是用勺子喂)...问题是我在频道日志中所做的部分,我在其中放入了频道 ID 而不是公会 ID
【讨论】:
以上是关于使用 json python 存储频道 ID 和公会 ID的主要内容,如果未能解决你的问题,请参考以下文章