Skip to content

Commit 25ecd0e

Browse files
author
Ivan Dardi
committed
fix whitelist checks + run black
1 parent a114168 commit 25ecd0e

6 files changed

Lines changed: 809 additions & 758 deletions

File tree

bot/__init__.py

Lines changed: 67 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,67 @@
1-
import datetime
2-
import logging
3-
import traceback
4-
import asyncio
5-
6-
import discord
7-
from discord.ext import commands
8-
9-
from bot.guild_caches import GuildEmojis, GuildRoles
10-
11-
log = logging.getLogger(__name__)
12-
13-
14-
class RustBot(commands.Bot):
15-
def __init__(self, *args, **kwargs):
16-
super().__init__(*args, **kwargs)
17-
18-
self.uptime = datetime.datetime.utcnow()
19-
self.config = kwargs["config"]
20-
self.custom_extensions = self.config["extensions"]
21-
self.guild = None
22-
self.emoji = None
23-
self.role = None
24-
25-
for extension in self.custom_extensions:
26-
try:
27-
self.load_extension(extension)
28-
except Exception as e: # noqa
29-
log.error("Failed to load extension %s\n%s: %s", extension, type(e).__name__, e)
30-
31-
async def on_ready(self):
32-
log.info("Logged in as %s", self.user)
33-
34-
await self.change_presence(activity=discord.Game(name="?help"))
35-
36-
self.guild = await self.fetch_guild(self.config["guild_id"])
37-
38-
self.emoji = GuildEmojis(self.config["emojis"], self.guild.emojis)
39-
self.role = GuildRoles(self.config["roles"], self.guild.roles)
40-
41-
async def on_command(self, ctx):
42-
destination = f"#{ctx.channel}"
43-
if isinstance(ctx.channel, discord.abc.PrivateChannel):
44-
destination += f" ({ctx.guild})"
45-
log.info(f"{ctx.author} in {destination}: {ctx.message.content}")
46-
47-
async def on_command_error(self, ctx: commands.Context, error):
48-
tb = "".join(
49-
traceback.format_exception(type(error), error, error.__traceback__)
50-
)
51-
log.error(f"Command error in %s:\n%s", ctx.command, tb)
52-
await ctx.message.add_reaction("❌")
53-
if isinstance(error, (commands.CheckFailure, commands.ConversionError)):
54-
await ctx.send(str(error))
55-
56-
async def on_member_join(self, member: discord.Member):
57-
if member.guild == self.guild:
58-
# Await half an hour before giving the Rustacean role to newcomers.
59-
await asyncio.sleep(1800)
60-
await member.add_roles(
61-
self.role.rustacean, reason=f"You have been automatically rusted! owo"
62-
)
1+
import datetime
2+
import logging
3+
import traceback
4+
import asyncio
5+
6+
import discord
7+
from discord.ext import commands
8+
9+
from bot.guild_caches import GuildEmojis, GuildRoles
10+
11+
log = logging.getLogger(__name__)
12+
13+
14+
class RustBot(commands.Bot):
15+
def __init__(self, *args, **kwargs):
16+
super().__init__(*args, **kwargs)
17+
18+
self.uptime = datetime.datetime.utcnow()
19+
self.config = kwargs["config"]
20+
self.custom_extensions = self.config["extensions"]
21+
self.guild = None
22+
self.emoji = None
23+
self.role = None
24+
25+
for extension in self.custom_extensions:
26+
try:
27+
self.load_extension(extension)
28+
except Exception as e: # noqa
29+
log.error(
30+
"Failed to load extension %s\n%s: %s",
31+
extension,
32+
type(e).__name__,
33+
e,
34+
)
35+
36+
async def on_ready(self):
37+
log.info("Logged in as %s", self.user)
38+
39+
await self.change_presence(activity=discord.Game(name="?help"))
40+
41+
self.guild = await self.fetch_guild(self.config["guild_id"])
42+
43+
self.emoji = GuildEmojis(self.config["emojis"], self.guild.emojis)
44+
self.role = GuildRoles(self.config["roles"], self.guild.roles)
45+
46+
async def on_command(self, ctx):
47+
destination = f"#{ctx.channel}"
48+
if isinstance(ctx.channel, discord.abc.PrivateChannel):
49+
destination += f" ({ctx.guild})"
50+
log.info(f"{ctx.author} in {destination}: {ctx.message.content}")
51+
52+
async def on_command_error(self, ctx: commands.Context, error):
53+
tb = "".join(
54+
traceback.format_exception(type(error), error, error.__traceback__)
55+
)
56+
log.error(f"Command error in %s:\n%s", ctx.command, tb)
57+
if isinstance(error, (commands.CheckFailure, commands.ConversionError)):
58+
await ctx.message.add_reaction("❌")
59+
await ctx.send(str(error))
60+
61+
async def on_member_join(self, member: discord.Member):
62+
if member.guild == self.guild:
63+
# Await half an hour before giving the Rustacean role to newcomers.
64+
await asyncio.sleep(1800)
65+
await member.add_roles(
66+
self.role.rustacean, reason=f"You have been automatically rusted! owo"
67+
)

bot/cogs/commands.py

Lines changed: 93 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,93 @@
1-
import datetime
2-
3-
import discord
4-
from discord.ext import commands
5-
6-
from bot import RustBot
7-
8-
9-
class Commands(commands.Cog):
10-
def __init__(self, bot: RustBot):
11-
self.bot = bot
12-
13-
@commands.command()
14-
async def uptime(self, ctx: commands.Context):
15-
"""Tells you how long the bot has been up for."""
16-
17-
now = datetime.datetime.utcnow()
18-
delta = now - self.bot.uptime
19-
hours, remainder = divmod(int(delta.total_seconds()), 3600)
20-
minutes, seconds = divmod(remainder, 60)
21-
days, hours = divmod(hours, 24)
22-
23-
fmt = "{h}h {m}m {s}s"
24-
if days:
25-
fmt = "{d}d " + fmt
26-
27-
await ctx.send(
28-
content="Uptime: {}".format(
29-
fmt.format(d=days, h=hours, m=minutes, s=seconds)
30-
)
31-
)
32-
33-
@commands.command()
34-
async def invite(self, ctx: commands.Context):
35-
"""Points the user to the #informational channel,
36-
which contains invite links.
37-
"""
38-
39-
channel = "<#273547351929520129>"
40-
link = "https://discordapp.com/channels/273534239310479360/273547351929520129/288101969980162049"
41-
await ctx.send(f"Invite links are provided in {channel}\n{link}")
42-
43-
@commands.command(aliases=["wustify"])
44-
@commands.guild_only()
45-
async def rustify(self, ctx: commands.Context, *members: discord.Member):
46-
"""Adds the Rustacean role to a member.
47-
Takes in a space-separated list of member mentions and/or IDs.
48-
"""
49-
50-
for member in members:
51-
await member.add_roles(
52-
self.bot.role.rustacean,
53-
reason=f"You have been rusted by {ctx.author}! owo",
54-
)
55-
56-
await ctx.message.add_reaction(self.bot.emoji.ok)
57-
58-
@commands.command()
59-
async def cleanup(self, ctx: commands.Context, limit=None):
60-
"""Deletes the bot's messages for cleanup.
61-
You can specify how many messages to look for.
62-
"""
63-
64-
limit = limit or 100
65-
66-
def is_me(m):
67-
return m.author.id == self.bot.user.id
68-
69-
deleted = await ctx.channel.purge(limit=limit, check=is_me)
70-
await ctx.send(f"Deleted {len(deleted)} message(s)", delete_after=5)
71-
await ctx.message.add_reaction(self.bot.emoji.ok)
72-
73-
@commands.command()
74-
async def source(self, ctx: commands.Context):
75-
"""Links to the bot GitHub repo."""
76-
77-
await ctx.send("https://github.com/ivandardi/RustbotPython")
78-
79-
@commands.command()
80-
async def ban(self, ctx: commands.Context, member: discord.Member):
81-
"""Bans another person."""
82-
await ctx.send(f"{ctx.author} banned user {member} <:ferrisBanne:419884768256327680>")
83-
await ctx.message.add_reaction(self.bot.emoji.ok)
84-
85-
async def cog_command_error(self, ctx: commands.Context, error):
86-
await ctx.message.clear_reactions()
87-
await ctx.message.add_reaction("❌")
88-
89-
90-
def setup(bot):
91-
bot.add_cog(Commands(bot))
1+
import datetime
2+
3+
import discord
4+
from discord.ext import commands
5+
6+
from bot import RustBot
7+
8+
9+
class Commands(commands.Cog):
10+
def __init__(self, bot: RustBot):
11+
self.bot = bot
12+
13+
@commands.command()
14+
async def uptime(self, ctx: commands.Context):
15+
"""Tells you how long the bot has been up for."""
16+
17+
now = datetime.datetime.utcnow()
18+
delta = now - self.bot.uptime
19+
hours, remainder = divmod(int(delta.total_seconds()), 3600)
20+
minutes, seconds = divmod(remainder, 60)
21+
days, hours = divmod(hours, 24)
22+
23+
fmt = "{h}h {m}m {s}s"
24+
if days:
25+
fmt = "{d}d " + fmt
26+
27+
await ctx.send(
28+
content="Uptime: {}".format(
29+
fmt.format(d=days, h=hours, m=minutes, s=seconds)
30+
)
31+
)
32+
33+
@commands.command()
34+
async def invite(self, ctx: commands.Context):
35+
"""Points the user to the #informational channel,
36+
which contains invite links.
37+
"""
38+
39+
channel = "<#273547351929520129>"
40+
link = "https://discordapp.com/channels/273534239310479360/273547351929520129/288101969980162049"
41+
await ctx.send(f"Invite links are provided in {channel}\n{link}")
42+
43+
@commands.command(aliases=["wustify"])
44+
@commands.guild_only()
45+
async def rustify(self, ctx: commands.Context, *members: discord.Member):
46+
"""Adds the Rustacean role to a member.
47+
Takes in a space-separated list of member mentions and/or IDs.
48+
"""
49+
50+
for member in members:
51+
await member.add_roles(
52+
self.bot.role.rustacean,
53+
reason=f"You have been rusted by {ctx.author}! owo",
54+
)
55+
56+
await ctx.message.add_reaction(self.bot.emoji.ok)
57+
58+
@commands.command()
59+
async def cleanup(self, ctx: commands.Context, limit=None):
60+
"""Deletes the bot's messages for cleanup.
61+
You can specify how many messages to look for.
62+
"""
63+
64+
limit = limit or 100
65+
66+
def is_me(m):
67+
return m.author.id == self.bot.user.id
68+
69+
deleted = await ctx.channel.purge(limit=limit, check=is_me)
70+
await ctx.send(f"Deleted {len(deleted)} message(s)", delete_after=5)
71+
await ctx.message.add_reaction(self.bot.emoji.ok)
72+
73+
@commands.command()
74+
async def source(self, ctx: commands.Context):
75+
"""Links to the bot GitHub repo."""
76+
77+
await ctx.send("https://github.com/ivandardi/RustbotPython")
78+
79+
@commands.command()
80+
async def ban(self, ctx: commands.Context, member: discord.Member):
81+
"""Bans another person."""
82+
await ctx.send(
83+
f"{ctx.author} banned user {member} <:ferrisBanne:419884768256327680>"
84+
)
85+
await ctx.message.add_reaction(self.bot.emoji.ok)
86+
87+
async def cog_command_error(self, ctx: commands.Context, error):
88+
await ctx.message.clear_reactions()
89+
await ctx.message.add_reaction("❌")
90+
91+
92+
def setup(bot):
93+
bot.add_cog(Commands(bot))

0 commit comments

Comments
 (0)