Skip to content

Commit 4ad90f8

Browse files
Minor bot & command updates
Co-authored-by: David Tso <davidtso1219@gmail.com>
1 parent 63836dd commit 4ad90f8

File tree

18 files changed

+404
-248
lines changed

18 files changed

+404
-248
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ Install [`Python 3.7+`](https://www.python.org/) and [`poetry`](https://python-p
3232
poetry install
3333
```
3434

35+
### Configuration
36+
37+
Create a file called `.env` with the following content:
38+
39+
```env
40+
BOT_TOKEN=<discord bot token>
41+
BOT_OWNERS=<comma separated Discord user IDs>
42+
BOT_PREFIX=<command prefix>
43+
```
44+
3545
### Start
3646

3747
Run the following command to start both bots:

bots/js/commands/find.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const fetch = require('node-fetch')
55

66
const { API_URL } = require('../config')
77
const getUsage = require('../utils/getUsage')
8+
const replaceCampusNames = require('../utils/replaceCampusNames')
89

910
// const days = ['M', 'T', 'W', 'Th', 'F', 'S', 'U']
1011
const days = ['M', 'T', 'W', 'R', 'F', 'S', 'U']
@@ -86,19 +87,15 @@ class FindCommand extends Command {
8687
}
8788

8889
exec(message, args) {
89-
console.log(
90-
`Find: ${message.author.username}#${message.author.discriminator}`,
91-
args
92-
)
93-
94-
args.campus && (args.campus = args.campus.toLowerCase())
90+
args.campus &&
91+
(args.campus = replaceCampusNames(args.campus.toLowerCase()))
9592
args.dept && (args.dept = args.dept.toUpperCase())
9693
args.course && (args.course = args.course.toUpperCase())
9794

9895
args.term && (args.term = args.term.toLowerCase())
9996

10097
if (!args.term && !args.year) {
101-
args.term = 'spring'
98+
args.term = 'fall'
10299
args.year = 2021
103100
}
104101

bots/js/commands/help.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ const EXTERNAL_COMMANDS = [
1616
],
1717
},
1818
},
19+
{
20+
id: 'ucstats',
21+
aliases: ['ucstats'],
22+
description: {
23+
text: 'Search UC Transfer Stats',
24+
usage: 'ucx for target major',
25+
examples: [
26+
{ name: '', cmd: 'ucb for bio' },
27+
{ name: '', cmd: 'ucla for comp sci' },
28+
],
29+
},
30+
},
31+
{
32+
id: 'grade',
33+
aliases: ['grade'],
34+
description: {
35+
text: 'Search grade distributions at Foothill College.',
36+
usage: 'professor for subject [course]',
37+
examples: [
38+
{ name: '', cmd: 'allison herman for everything' },
39+
{ name: '', cmd: 'cascarano for phys 4a' },
40+
],
41+
},
42+
},
1943
]
2044

2145
class HelpCommand extends Command {
@@ -41,6 +65,8 @@ class HelpCommand extends Command {
4165
.setFooter('Hope you enjoy!')
4266

4367
const addCommandToHelp = (command) => {
68+
if (command.description.hidden) return
69+
4470
let description = command.description.text || 'No description'
4571

4672
description +=

bots/js/commands/rate.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Discord = require('discord.js')
33
const fetch = require('node-fetch')
44

55
const getUsage = require('../utils/getUsage')
6+
const replaceCampusNames = require('../utils/replaceCampusNames')
67

78
function getRMPUrl(campus, q) {
89
let schoolName, schoolId
@@ -129,7 +130,7 @@ class RateCommand extends Command {
129130
return
130131
}
131132

132-
args.campus = args.campus.toLowerCase()
133+
args.campus = replaceCampusNames(args.campus.toLowerCase())
133134

134135
if (!['fh', 'da'].includes(args.campus)) {
135136
message.util.send(
@@ -171,10 +172,8 @@ class RateCommand extends Command {
171172
others.length &&
172173
`*Also found:* ${others
173174
.map(
174-
(prof) =>
175-
`${prof['teacherfullname_s']} (${
176-
prof['averageratingscore_rf'] || '?'
177-
})`
175+
({ teacherfullname_s, averageratingscore_rf }) =>
176+
`${teacherfullname_s} (${averageratingscore_rf || '?'})`
178177
)
179178
.join(', ')}`
180179

bots/js/commands/reload.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const { Command } = require('discord-akairo')
2+
3+
class ReloadCommand extends Command {
4+
constructor() {
5+
super('reload-js', {
6+
aliases: ['reload-js', 'rjs'],
7+
description: {
8+
hidden: true,
9+
text: 'Reload a command handler',
10+
usage: '<command>',
11+
},
12+
args: [
13+
{
14+
id: 'command',
15+
type: 'string',
16+
default: 'all',
17+
},
18+
],
19+
ownerOnly: true,
20+
category: 'owner',
21+
})
22+
}
23+
24+
exec(message, args) {
25+
if (args.command == 'all') {
26+
this.handler.reloadAll()
27+
return message.channel.send(
28+
`Reloaded ${
29+
Array.from(this.handler.modules).length
30+
} command(s)!`
31+
)
32+
}
33+
34+
try {
35+
this.handler.reload(args.command)
36+
return message.util.send(`Reloaded command ${args.command}!`)
37+
} catch {
38+
try {
39+
const category = this.handler.categories
40+
.map((c) => c)
41+
.filter((c) => c == args.command)[0]
42+
category.reloadAll()
43+
44+
return message.util.send(
45+
`Reloaded category ${args.command}!\nReloaded ${
46+
Array.from(category).length
47+
} command(s)!`
48+
)
49+
} catch (e) {
50+
return message.util.send(
51+
`"${args.command}" is not a valid category/command ID!`
52+
)
53+
}
54+
}
55+
}
56+
}
57+
58+
module.exports = ReloadCommand
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { Listener } = require('discord-akairo')
2+
3+
class CommandStartedListener extends Listener {
4+
constructor() {
5+
super('commandStarted', {
6+
emitter: 'commandHandler',
7+
event: 'commandStarted',
8+
})
9+
}
10+
11+
exec(message, command) {
12+
console.log(
13+
`[cmd] ${message.author.tag} invoked "${command.id}" with "${message.content}"`
14+
)
15+
}
16+
}
17+
18+
module.exports = CommandStartedListener
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const replacements = {
2+
foothill: 'fh',
3+
'de-anza': 'da',
4+
'de anza': 'da',
5+
deanza: 'da',
6+
}
7+
8+
const replaceCampusNames = (name) => replacements[name] || name
9+
10+
module.exports = replaceCampusNames

bots/py/bot.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
from os import getenv
2-
from discord.ext import commands
2+
from discord.ext.commands import Bot
33
from dotenv import load_dotenv
44

55
load_dotenv()
66

7-
8-
class Bot(commands.Bot):
9-
def before_invoke(self, ctx):
10-
print(
11-
f'[command] "{ctx.author}" invoked "{ctx.command.name}" with "{ctx.message.content}"'
12-
)
13-
14-
15-
client = Bot(command_prefix=getenv("BOT_PREFIX", "?"))
16-
7+
bot = Bot(command_prefix=getenv("BOT_PREFIX", "?"))
178
extensions = [
189
"commands.admin",
1910
"commands.assist",
@@ -24,8 +15,14 @@ def before_invoke(self, ctx):
2415
"commands.grade",
2516
]
2617

18+
@bot.before_invoke
19+
async def before_invoke(ctx):
20+
print(
21+
f'[cmd] {ctx.author} invoked "{ctx.command.name}" with "{ctx.message.content}"'
22+
)
23+
2724
if __name__ == "__main__":
28-
client.remove_command("help")
25+
bot.remove_command("help")
2926
for extension in extensions:
30-
client.load_extension(extension)
31-
client.run(getenv("BOT_TOKEN"))
27+
bot.load_extension(extension)
28+
bot.run(getenv("BOT_TOKEN"))

bots/py/commands/admin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ async def reload(self, ctx, ext_name: str = ""):
3636
except commands.errors.ExtensionNotLoaded:
3737
await ctx.send("Extension not loaded")
3838

39+
@commands.command()
40+
async def stats(self, ctx):
41+
server_list = '\n'.join([guild.name for guild in self.bot.guilds])
42+
43+
await ctx.send(f"__**Servers**__\n{server_list}")
44+
3945

4046
def setup(bot):
4147
bot.add_cog(Admin(bot))

bots/py/commands/assist.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import requests
2-
import asyncio
32
import discord
43

54
from discord.ext import commands
65

76
from college import College
87
from major import Major
9-
from emojis import emojis, numeric_emojis, num_emojis
10-
from page import Page
11-
from utils import pad_z
8+
from emojis import emojis
129
from selection import select
1310

1411
url = "https://assist.org/api/institutions"

0 commit comments

Comments
 (0)