Skip to content

Commit

Permalink
Merge pull request #41 from paiza-learning/feat/update-discordjs
Browse files Browse the repository at this point in the history
discordjs v13 への移行
  • Loading branch information
cwd-k2 authored Dec 2, 2022
2 parents 2c28826 + 9eda8c8 commit 7b90e0f
Show file tree
Hide file tree
Showing 26 changed files with 812 additions and 1,566 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:14 as production
FROM node:17 as production

ENV NODE_ENV=production
WORKDIR /usr/src/app
Expand Down
33 changes: 19 additions & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@ version: '3'
services:
warrior:
container_name: warrior
image: node:12
image: node:18
working_dir: /usr/src/app
volumes:
- ./:/usr/src/app
- node_modules:/usr/src/app/node_modules
command: [yarn, watch]
db:
image: postgres:latest
ports:
- 5432:5432
volumes:
- postgres:/var/lib/postgresql/data
- ./db/initdb:/docker-entrypoint-initdb.d
environment:
- POSTGRES_USER=paiza
- POSTGRES_PASSWORD=learning
- POSTGRES_DB=warriors
command:
- /bin/bash
- -c
- |
yarn
yarn watch
# db:
# image: postgres:latest
# ports:
# - 5432:5432
# volumes:
# - postgres:/var/lib/postgresql/data
# - ./db/initdb:/docker-entrypoint-initdb.d
# environment:
# - POSTGRES_USER=paiza
# - POSTGRES_PASSWORD=learning
# - POSTGRES_DB=warriors

volumes:
node_modules:
postgres:
# postgres:
4 changes: 2 additions & 2 deletions heroku.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ build:
worker: Dockerfile
release:
image: worker
command:
- yarn typeorm migration:run
# command:
# - yarn typeorm migration:run
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@
"nodemon": "^2.0.6",
"prettier": "^2.2.0",
"rimraf": "^3.0.2",
"typescript": "^4.1.2"
"typescript": "^4.6.2"
},
"dependencies": {
"debug": "^4.3.1",
"discord.js": "^12.5.1",
"discord.js-commando": "^0.12.2",
"discord.js": "^13.6.0",
"dotenv": "^8.2.0",
"js-yaml": "^3.14.1",
"node-fetch": "^2.6.1",
"pg": "^8.5.1",
"reflect-metadata": "^0.1.13",
"ts-node": "^9.1.1",
"ts-node": "^10.6.0",
"typeorm": "^0.2.31"
}
}
40 changes: 14 additions & 26 deletions src/commands/fun/moja.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
import {
Command,
CommandInfo,
CommandoClient,
CommandoMessage,
} from 'discord.js-commando';
import { Command } from '../../models/command';

import fetch from 'node-fetch';
import yaml from 'js-yaml';
import debug from 'debug';

const mojamojaURL = 'https://www.mojamoja.cloud/api/v1/environment/latest';

export default class MojaCommand extends Command {
constructor(client: CommandoClient) {
super(client, {
name: 'moja',
group: 'fun',
memberName: 'moja',
description: 's10akir の自室の環境情報を得ることができます.',
} as CommandInfo);
}

async run(msg: CommandoMessage): Promise<CommandoMessage> {
const result = await fetch(mojamojaURL, { method: 'GET' })
.then((resp) => resp.json())
.then((data) => {
debug(data);
return '```yaml\n' + yaml.dump(data) + '```';
});

return msg.say(result);
}
async function main() {
return await fetch(mojamojaURL, { method: 'GET' })
.then((resp) => resp.json())
.then((data) => {
debug(data);
return '```yaml\n' + yaml.dump(data) + '```';
});
}

Command.register('moja', {
desc: '@s10akir の自室の環境情報を取得します。',
exec: main,
help: '@s10akir の自室の環境情報を取得します。',
});
44 changes: 16 additions & 28 deletions src/commands/fun/shibe.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
import {
Command,
CommandInfo,
CommandoClient,
CommandoMessage,
} from 'discord.js-commando';

import { Command } from '../../models/command';
import { Message, MessageAttachment } from 'discord.js';

import fetch from 'node-fetch';
import debug from 'debug';

const shibeURL = 'http://shibe.online/api/shibes?count=1]&urls=true';

export default class ShibeCommand extends Command {
constructor(client: CommandoClient) {
super(client, {
name: 'shibe',
group: 'fun',
memberName: 'shibe',
description: 'わんこの画像が見れるわん',
} as CommandInfo);
}

async run(msg: CommandoMessage): Promise<Message> {
const result = await fetch(shibeURL, { method: 'GET' })
.then((res) => res.json())
.then((data) => {
debug(data[0]);
return data[0];
});
const attachment = new MessageAttachment(result);

return msg.channel.send(attachment);
}
async function main(_1: string[], _2: string, msg: Message) {
const result = await fetch(shibeURL, { method: 'GET' })
.then((res) => res.json())
.then((data) => {
debug(data[0]);
return data[0];
});
const attachment = new MessageAttachment(result);
msg.channel.send({ files: [attachment] });
}

Command.register('shibe', {
desc: 'わんこの画像が見れるわん',
exec: main,
help: 'わんこの画像が見れるわん',
});
11 changes: 11 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import './fun/moja';
import './fun/shibe';

import './paiza/hello';
import './paiza/paizaIO';
import './paiza/reactme';

import './utility/help';
// import './utility/query';

// import './todo';
26 changes: 6 additions & 20 deletions src/commands/paiza/hello.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
import {
Command,
CommandInfo,
CommandoClient,
CommandoMessage,
} from 'discord.js-commando';
import { Command } from '../../models/command';

export default class HelloCommand extends Command {
constructor(client: CommandoClient) {
super(client, {
name: 'hello',
group: 'paiza',
memberName: 'hello',
description: 'hello',
} as CommandInfo);
}

run(msg: CommandoMessage): Promise<CommandoMessage> {
return msg.say('hello');
}
}
Command.register('hello', {
desc: 'hello',
exec: async () => 'hello',
help: 'hello',
});
43 changes: 13 additions & 30 deletions src/commands/paiza/paizaIO.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
Command,
CommandInfo,
CommandoClient,
CommandoMessage,
} from 'discord.js-commando';
import { Command } from '../../models/command';

import fetch from 'node-fetch';
import yaml from 'js-yaml';
Expand All @@ -12,30 +7,6 @@ import Constants from '../../constants';

const API_KEY = Constants.paizaIO.API_KEY;

export default class PaizaIOCommand extends Command {
constructor(client: CommandoClient) {
super(client, {
name: 'paizaio',
group: 'paiza',
memberName: 'paizaio',
description: 'paiza.io の API を利用して, コードを実行します.',
} as CommandInfo);
}

async run(msg: CommandoMessage): Promise<null> {
const content = msg.content;
const firstline = content.split('\n', 1)[0];
const [, ...args] = firstline.split(' ');
const contentBody = content.substr(firstline.length + 1);

const result = await runCode(args, contentBody);

msg.reply(result);

return null;
}
}

/**
* paiza.io API を叩いて contentBody に渡されたコードを実行してもらう
* args[0] が言語名
Expand Down Expand Up @@ -105,3 +76,15 @@ async function runCode(args: string[], contentBody: string): Promise<string> {

return runnersGetDetailsResponse.stdout;
}

Command.register('paizaio', {
desc: 'paiza.io の API を利用して、与えられたコードを実行します。',
exec: async (args, contentBody, msg) => {
msg.reply(await runCode(args, contentBody));
},
help: `usage: paizaio <language> [--detail]
\\\`\\\`\\\`
// your code here
\\\`\\\`\\\`
<stdin-string>`,
});
40 changes: 8 additions & 32 deletions src/commands/paiza/reactme.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
import {
Command,
CommandInfo,
CommandoClient,
CommandoMessage,
} from 'discord.js-commando';
import { Command } from '../../models/command';

interface ReactMeArgument {
emoji: string;
}

export default class ReactMeCommand extends Command {
constructor(client: CommandoClient) {
super(client, {
name: 'reactme',
group: 'paiza',
memberName: 'reactme',
description: '投稿に指定したリアクションをしてくれます.',
args: [
{
key: 'emoji',
prompt: 'What would you like me to react with?',
type: 'string',
},
],
} as CommandInfo);
}

async run(msg: CommandoMessage, args: ReactMeArgument): Promise<null> {
msg.react(args.emoji);
return null;
}
}
Command.register('reactme', {
desc: '投稿に指定したリアクションをしてくれます',
exec: async (args, _, msg) => {
msg.react(args[0]);
},
help: '投稿に指定したリアクションをしてくれます',
});
77 changes: 77 additions & 0 deletions src/commands/todo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Command } from '../models/command';
import { Todo } from '../entities/todo';
import { Message } from 'discord.js';

async function main(args: string[], _: string, msg: Message) {
switch (args[0]) {
case 'add':
addTodo(args, msg);
break;
case 'del':
delTodo(args, msg);
break;
case 'list':
listTodo(msg);
break;
}
}

async function addTodo(args: string[], msg: Message) {
const userId = msg.author.id;

const todo = new Todo();
todo.userId = userId;
todo.title = args[0];

await todo.save();

return msg.reply(`success. [${todo.title}]`);
}

async function delTodo(args: string[], msg: Message) {
const todo = await Todo.findOne(args[0]);
const userId = msg.author.id;

if (!todo) {
return msg.reply('[!] todo does not exists.');
}

if (todo.userId !== userId) {
return msg.reply('[!] you are not owner of this todo.');
}

const todo_title = todo.title;
await todo.remove(); // DBの容量節約にレコードを物理削除

return msg.reply(`success! remove [${todo_title}]`);
}

async function listTodo(msg: Message) {
const user = msg.author;

const todos = await Todo.find({ userId: user.id });
let text: string;

if (todos.length !== 0) {
const todoList = [`${user.username}'s TODO LIST:`];
todos.forEach((todo) => {
todoList.push(`- ${todo.id}: ${todo.title}`);
});
text = todoList.join('\n');
} else {
text = ':tada: LGTM! :tada: WE ARE FREE!';
}

return msg.reply(text);
}

Command.register('todo', {
desc: 'manage your todo',
exec: main,
help: `usage todo [add|del|list]
add: add todo
todo add [title]
del: delete todo
todo del [id]
list: list todo`,
});
Loading

0 comments on commit 7b90e0f

Please sign in to comment.