Skip to content

Instantly share code, notes, and snippets.

@Pepijn98
Last active January 9, 2018 22:05
Show Gist options
  • Save Pepijn98/bf4ffb07123d363cac61d5e7eab923da to your computer and use it in GitHub Desktop.
Save Pepijn98/bf4ffb07123d363cac61d5e7eab923da to your computer and use it in GitHub Desktop.
discord.py command to save modlogs to a yaml file. (Quick solution to a problem, better to use a database)
@commands.guild_only()
@commands.has_permissions(manage_guild=True)
@commands.command(name='modlog')
async def set_modlog(self, ctx, channel: discord.TextChannel):
"""Set the modlog channel"""
with open('db/guild_settings.yaml', 'r+') as yaml_file:
yaml_data = yaml.load(yaml_file)
keys = list(yaml_data.keys())
if f'{ctx.guild.id}' in keys:
if yaml_data[f'{ctx.guild.id}']['modlog'] == str(channel.id):
await ctx.send('Channel already set as modlog.')
return yaml_file.close()
else:
with open('db/guild_settings.yaml', 'w') as f:
yaml_data[f'{ctx.guild.id}']['modlog'] = str(channel.id)
f.write(yaml.dump(yaml_data, default_flow_style=False))
await ctx.send(f'Updated modlog to `{channel}`')
yaml_file.close()
f.close()
else:
data = {
f'{ctx.guild.id}': {
'modlog': f'{channel.id}'
}
}
yaml.dump(data, yaml_file, default_flow_style=False)
await ctx.send(f'Added `{channel}` as modlog')
yaml_file.close()
######################################################################################################
######################################################################################################
def get_modlog(ctx):
try:
with open('db/guild_settings.yaml', 'r') as yaml_file:
data = yaml.load(yaml_file)
return data[f'{ctx.guild.id}']['modlog']
except KeyError:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment