# Buggy code
async def _set(self, ctx):
    """Changes Red's settings"""
    if ctx.invoked_subcommand is None:
        admin_role_id = await ctx.bot.db.guild(ctx.guild).admin_role()
        admin_role = discord.utils.get(ctx.guild.roles, id=admin_role_id)
        mod_role_id = await ctx.bot.db.guild(ctx.guild).mod_role()
        mod_role = discord.utils.get(ctx.guild.roles, id=mod_role_id)
        prefixes = await ctx.bot.db.guild(ctx.guild).prefix()
        if not prefixes:
            prefixes = await ctx.bot.db.prefix()
        locale = await ctx.bot.db.locale()

        settings = (
            "{} Settings:\n\n"
            "Prefixes: {}\n"
            "Admin role: {}\n"
            "Mod role: {}\n"
            "Locale: {}"
            "".format(
                ctx.bot.user.name,
                " ".join(prefixes),
                admin_role.name if admin_role else "Not set",
                mod_role.name if mod_role else "Not set",
                locale,
            )
        )
        await ctx.send(box(settings))
        await ctx.send_help()


# Traceback (AttributeError)
Exception in command 'set'
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 62, in wrapped
ret = yield from coro(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/redbot/core/core_commands.py", line 518, in _set
admin_role_id = await ctx.bot.db.guild(ctx.guild).admin_role()
File "/usr/local/lib/python3.6/dist-packages/redbot/core/config.py", line 760, in guild
return self._get_base_group(self.GUILD, guild.id)
AttributeError: 'NoneType' object has no attribute 'id'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/bot.py", line 886, in invoke
yield from ctx.command.invoke(ctx)
File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 923, in invoke
yield from injected(*ctx.args, **ctx.kwargs)
File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 71, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'id'

# Fixed code
async def _set(self, ctx):
    """Changes Red's settings"""
    if ctx.invoked_subcommand is None:
        if ctx.guild:
            admin_role_id = await ctx.bot.db.guild(ctx.guild).admin_role()
            admin_role = (
                discord.utils.get(ctx.guild.roles, id=admin_role_id) or "Not set"
            )
            mod_role_id = await ctx.bot.db.guild(ctx.guild).mod_role()
            mod_role = discord.utils.get(ctx.guild.roles, id=mod_role_id) or "Not set"
            prefixes = await ctx.bot.db.guild(ctx.guild).prefix()
            guild_settings = f"Admin role: {admin_role}\nMod role: {mod_role}\n"
        else:
            guild_settings = ""
        if not prefixes:
            prefixes = await ctx.bot.db.prefix()
        locale = await ctx.bot.db.locale()

        prefix_string = " ".join(prefixes)
        settings = (
            f"{ctx.bot.user.name} Settings:\n\n"
            f"Prefixes: {prefix_string}\n"
            f"{guild_settings}"
            f"Locale: {locale}"
        )
        await ctx.send(box(settings))
        await ctx.send_help()