Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit b53e696

Browse files
authored
Merge pull request #4 from treefroog/cogs
Cogs
2 parents 6dc576a + 5341fee commit b53e696

16 files changed

+650
-308
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/*
2+
credentials.json
3+
*.pyc
4+
*.log

cogs/admin.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from discord.ext import commands
2+
from .utils import checks
3+
import discord
4+
import inspect
5+
import asyncio
6+
7+
class Admin:
8+
"""Admin-only commands that make the bot dynamic."""
9+
10+
def __init__(self, bot):
11+
self.bot = bot
12+
13+
@commands.command(hidden=True)
14+
@checks.is_owner()
15+
async def load(self, *, module : str):
16+
"""Loads a module."""
17+
try:
18+
self.bot.load_extension(module)
19+
except Exception as e:
20+
await self.bot.say('\N{PISTOL}')
21+
await self.bot.say('{}: {}'.format(type(e).__name__, e))
22+
else:
23+
await self.bot.say('\N{OK HAND SIGN}')
24+
25+
@commands.command(hidden=True)
26+
@checks.is_owner()
27+
async def unload(self, *, module : str):
28+
"""Unloads a module."""
29+
try:
30+
self.bot.unload_extension(module)
31+
except Exception as e:
32+
await self.bot.say('\N{PISTOL}')
33+
await self.bot.say('{}: {}'.format(type(e).__name__, e))
34+
else:
35+
await self.bot.say('\N{OK HAND SIGN}')
36+
37+
@commands.command(name='reload', hidden=True)
38+
@checks.is_owner()
39+
async def _reload(self, *, module : str):
40+
"""Reloads a module."""
41+
try:
42+
self.bot.unload_extension(module)
43+
self.bot.load_extension(module)
44+
except Exception as e:
45+
await self.bot.say('\N{PISTOL}')
46+
await self.bot.say('{}: {}'.format(type(e).__name__, e))
47+
else:
48+
await self.bot.say('\N{OK HAND SIGN}')
49+
50+
@commands.command(pass_context=True, hidden=True)
51+
@checks.is_owner()
52+
async def debug(self, ctx, *, code : str):
53+
"""Evaluates code."""
54+
code = code.strip('` ')
55+
python = '```py\n{}\n```'
56+
result = None
57+
58+
env = {
59+
'bot': self.bot,
60+
'ctx': ctx,
61+
'message': ctx.message,
62+
'server': ctx.message.server,
63+
'channel': ctx.message.channel,
64+
'author': ctx.message.author
65+
}
66+
67+
try:
68+
result = eval(code, env)
69+
if inspect.isawaitable(result):
70+
result = await result
71+
except Exception as e:
72+
await self.bot.say(python.format(type(e).__name__ + ': ' + str(e)))
73+
return
74+
75+
await self.bot.say(python.format(result))
76+
77+
def setup(bot):
78+
bot.add_cog(Admin(bot))

cogs/meta.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from discord.ext import commands
2+
from .utils import checks, formats
3+
import discord
4+
from collections import OrderedDict, deque, Counter
5+
import os, datetime
6+
import re, asyncio
7+
import copy
8+
9+
10+
11+
class Meta:
12+
"""Commands for utilities related to Discord or the Bot itself."""
13+
14+
def __init__(self, bot):
15+
self.bot = bot
16+
17+
@commands.command(hidden=True)
18+
async def hello(self):
19+
"""Displays my hello message."""
20+
await self.bot.say('Hello! I\'m a bot made by <@149281074437029890>')
21+
22+
@commands.command(hidden=True)
23+
async def source(self):
24+
"""displays link to github"""
25+
await self.bot.say('Github: https://github.com/treefroog/tapir-bot')
26+
27+
@commands.command(rest_is_raw=True, hidden=True, aliases=['say'])
28+
@checks.is_owner()
29+
async def echo(self, *, content):
30+
"""says stuff that I tell it to"""
31+
await self.bot.say(content)
32+
33+
@commands.command(name='quit', hidden=True, aliases=['close'])
34+
@checks.is_owner()
35+
async def _quit(self):
36+
"""quits tapir-bot"""
37+
await self.bot.logout()
38+
39+
@commands.command(hidden=True)
40+
@checks.is_owner()
41+
async def commandstats(self):
42+
"""gives me command stats"""
43+
msg = 'Since startup, {} commands have been used.\n{}'
44+
counter = self.bot.commands_used
45+
await self.bot.say(msg.format(sum(counter.values()), counter))
46+
47+
def setup(bot):
48+
bot.add_cog(Meta(bot))

cogs/mod.py

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
from discord.ext import commands
2+
from .utils import config, checks
3+
from collections import Counter
4+
import re
5+
import discord
6+
import asyncio
7+
8+
class Mod:
9+
"""Moderation related commands."""
10+
11+
def __init__(self, bot):
12+
self.bot = bot
13+
self.config = config.Config('mod.json', loop=bot.loop)
14+
15+
def bot_user(self, message):
16+
return message.server.me if message.channel.is_private else self.bot.user
17+
18+
@commands.group(pass_context=True, no_pm=True, hidden=True)
19+
@checks.admin_or_permissions(manage_channels=True)
20+
async def ignore(self, ctx):
21+
"""Handles the bot's ignore lists.
22+
To use these commands, you must have the Bot Admin role or have
23+
Manage Channels permissions. These commands are not allowed to be used
24+
in a private message context.
25+
Users with Manage Roles or Bot Admin role can still invoke the bot
26+
in ignored channels.
27+
"""
28+
if ctx.invoked_subcommand is None:
29+
await self.bot.say('Invalid subcommand passed: {0.subcommand_passed}'.format(ctx))
30+
31+
@ignore.command(name='list', pass_context=True)
32+
async def ignore_list(self, ctx):
33+
"""Tells you what channels are currently ignored in this server."""
34+
35+
ignored = self.config.get('ignored', [])
36+
channel_ids = set(c.id for c in ctx.message.server.channels)
37+
result = []
38+
for channel in ignored:
39+
if channel in channel_ids:
40+
result.append('<#{}>'.format(channel))
41+
42+
if result:
43+
await self.bot.say('The following channels are ignored:\n\n{}'.format(', '.join(result)))
44+
else:
45+
await self.bot.say('I am not ignoring any channels here.')
46+
47+
@ignore.command(name='channel', pass_context=True)
48+
async def channel_cmd(self, ctx, *, channel : discord.Channel = None):
49+
"""Ignores a specific channel from being processed.
50+
If no channel is specified, the current channel is ignored.
51+
If a channel is ignored then the bot does not process commands in that
52+
channel until it is unignored.
53+
"""
54+
55+
if channel is None:
56+
channel = ctx.message.channel
57+
58+
ignored = self.config.get('ignored', [])
59+
if channel.id in ignored:
60+
await self.bot.say('That channel is already ignored.')
61+
return
62+
63+
ignored.append(channel.id)
64+
await self.config.put('ignored', ignored)
65+
await self.bot.say('\U0001f44c')
66+
67+
@ignore.command(name='all', pass_context=True)
68+
@checks.admin_or_permissions(manage_server=True)
69+
async def _all(self, ctx):
70+
"""Ignores every channel in the server from being processed.
71+
This works by adding every channel that the server currently has into
72+
the ignore list. If more channels are added then they will have to be
73+
ignored by using the ignore command.
74+
To use this command you must have Manage Server permissions along with
75+
Manage Channels permissions. You could also have the Bot Admin role.
76+
"""
77+
78+
ignored = self.config.get('ignored', [])
79+
channels = ctx.message.server.channels
80+
ignored.extend(c.id for c in channels if c.type == discord.ChannelType.text)
81+
await self.config.put('ignored', list(set(ignored))) # make unique
82+
await self.bot.say('\U0001f44c')
83+
84+
@commands.command(pass_context=True, no_pm=True)
85+
@checks.admin_or_permissions(manage_channels=True)
86+
async def unignore(self, ctx, *, channel : discord.Channel = None):
87+
"""Unignores a specific channel from being processed.
88+
If no channel is specified, it unignores the current channel.
89+
To use this command you must have the Manage Channels permission or have the
90+
Bot Admin role.
91+
"""
92+
93+
if channel is None:
94+
channel = ctx.message.channel
95+
96+
# a set is the proper data type for the ignore list
97+
# however, JSON only supports arrays and objects not sets.
98+
ignored = self.config.get('ignored', [])
99+
try:
100+
ignored.remove(channel.id)
101+
except ValueError:
102+
await self.bot.say('Channel was not ignored in the first place.')
103+
else:
104+
await self.bot.say('\U0001f44c')
105+
106+
@commands.command(no_pm=True)
107+
@checks.admin_or_permissions(manage_server=True)
108+
async def plonk(self, *, member : discord.Member):
109+
"""Bans a user from using the bot.
110+
Note that this ban is **global**. So they are banned from
111+
all servers that they access the bot with. So use this with
112+
caution.
113+
There is no way to bypass a plonk regardless of role or permissions.
114+
The only person who cannot be plonked is the bot creator. So this
115+
must be used with caution.
116+
To use this command you must have the Manage Server permission
117+
or have a Bot Admin role.
118+
"""
119+
120+
plonks = self.config.get('plonks', [])
121+
if member.id in plonks:
122+
await self.bot.say('That user is already bot banned.')
123+
return
124+
125+
plonks.append(member.id)
126+
await self.config.put('plonks', plonks)
127+
await self.bot.say('{0.name} has been banned from using the bot.'.format(member))
128+
129+
@commands.command(no_pm=True)
130+
@checks.admin_or_permissions(manage_server=True)
131+
async def unplonk(self, *, member : discord.Member):
132+
"""Unbans a user from using the bot.
133+
To use this command you must have the Manage Server permission
134+
or have a Bot Admin role.
135+
"""
136+
137+
plonks = self.config.get('plonks', [])
138+
139+
try:
140+
plonks.remove(member.id)
141+
except ValueError:
142+
pass
143+
else:
144+
await self.config.put('plonks', plonks)
145+
await self.bot.say('{0.name} has been unbanned from using the bot.'.format(member))
146+
147+
def setup(bot):
148+
bot.add_cog(Mod(bot))

cogs/ships.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from discord.ext import commands
2+
from .utils import checks
3+
import discord
4+
import asyncio
5+
6+
class Ships:
7+
"""all of the Star Citizen ship commands"""
8+
9+
def __init__(self, bot):
10+
self.bot = bot
11+
12+
@commands.group(pass_context=True)
13+
async def ship(self, ctx):
14+
"""Posts a link to an album of the specified ship (no where near done)"""
15+
if ctx.invoked_subcommand is None:
16+
await self.bot.say('Invalid ship: {0.subcommand_passed}'.format(ctx))
17+
18+
@ship.command(pass_context=True)
19+
async def carrack(self):
20+
await self.bot.say('Carrack pls http://i.imgur.com/BA3F1OI.png')
21+
22+
def setup(bot):
23+
bot.add_cog(Ships(bot))

cogs/star_citizen.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from discord.ext import commands
2+
from .utils import checks
3+
import discord
4+
import asyncio
5+
6+
class Star_Citizen:
7+
"""All of the Star Citizen related commands"""
8+
9+
def __init__(self, bot):
10+
self.bot = bot
11+
12+
@commands.command()
13+
async def ben(self):
14+
await self.bot.say('http://i.imgur.com/OLKOQ6H.gif')
15+
16+
@commands.command()
17+
async def scam(self):
18+
await self.bot.say('Star Citizen is a scam, confirmed by Chris Roberts himself: http://i.imgur.com/UK3D1c0.gifv')
19+
20+
@commands.command(name='2.4')
21+
async def two_four(self):
22+
await self.bot.say('It\'s not just a meme! http://i.imgur.com/umBUjqW.gif')
23+
24+
def setup(bot):
25+
bot.add_cog(Star_Citizen(bot))

cogs/tapir.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from discord.ext import commands
2+
from .utils import config, checks
3+
import random
4+
import discord
5+
import asyncio
6+
7+
class Tapir:
8+
"""the tapir command(s)"""
9+
10+
def __init__(self, bot):
11+
self.bot = bot
12+
13+
@commands.command()
14+
async def tapir(self):
15+
"""The wonderful tapir command that outputs a random tapir"""
16+
tapir_file = config.Config('tapirs.json')
17+
tapirs = tapir_file.get('tapirs')
18+
tapir = tapirs[random.randrange(len(tapirs))]
19+
await self.bot.say(tapir)
20+
21+
@commands.command(hidden=True)
22+
@checks.is_owner()
23+
async def save_tapir(self, *, tapir):
24+
"""allows the saving of a tapirs"""
25+
tapir_file = config.Config('tapirs.json', loop=bot.loop)
26+
tapirs = tapir_file.get('tapirs')
27+
tapirs.append(tapir)
28+
tapir_file.put(tapirs)
29+
30+
def setup(bot):
31+
bot.add_cog(Tapir(bot))

cogs/utils/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)