Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord v13 update #14

Merged
merged 21 commits into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 5 additions & 9 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es2021: true,
},
parser: "@typescript-eslint/parser",
extends: [
"airbnb-typescript/base",
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
],
plugins: ["@typescript-eslint", "prettier"],
extends: ["airbnb-typescript/base", "prettier"],
plugins: ["@typescript-eslint", "prettier", "import"],
parserOptions: {
ecmaVersion: 12,
project: "./tsconfig.json",
Expand All @@ -24,5 +17,8 @@ module.exports = {
semi: 2,
"max-len": 0,
"no-prototype-builtins": 0,
"@typescript-eslint/no-unused-expressions": 0,
"class-methods-use-this": 0,
"no-non-null-assertion": 0,
},
};
3 changes: 3 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tasks:
- name: Install dependencies
init: nvm install 16 && npm i
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Discord Speech Recognition Extension

This is an extension for [discord.js](https://discord.js.org) library that makes creating discord speech recognition bots as easy as common text bots.

## Installation

`npm i discord-speech-recognition`

You need also dependency for voice, recommended:
`npm i @discordjs/opus`
You can read more here: <https://discordjs.guide/voice/#installing-dependencies>

## Docs

<https://discordsr.netlify.app/>

## Example usage

```javascript
const { Client, Message } = require("discord.js");
const { addSpeechEvent } = require("discord-speech-recognition");

const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES],
});
addSpeechEvent(client);

client.on("messageCreate", (msg) => {
const voiceChannel = msg.member?.voice.channel;
if (voiceChannel) {
joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
selfDeaf: false,
});
}
});

client.on("speech", (msg) => {
msg.author.send(msg.content);
});

client.login("token");
```
2 changes: 1 addition & 1 deletion docs/readme.md → docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ This is an extension for [discord.js](https://discord.js.org) library that makes
## Example usage

```javascript
[[include:simpleBot.js]]
[[include:simpleBot/index.js]]
```
33 changes: 0 additions & 33 deletions examples/advancedBot.ts

This file was deleted.

17 changes: 0 additions & 17 deletions examples/simpleBot.js

This file was deleted.

25 changes: 25 additions & 0 deletions examples/simpleBot/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { Client, Message } = require("discord.js");
const { addSpeechEvent } = require("discord-speech-recognition");

const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES],
});
addSpeechEvent(client);

client.on("messageCreate", (msg) => {
const voiceChannel = msg.member?.voice.channel;
if (voiceChannel) {
joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
selfDeaf: false,
});
}
});

client.on("speech", (msg) => {
msg.author.send(msg.content);
});

client.login("token");
16 changes: 16 additions & 0 deletions examples/simpleBot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "simplebot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"discord-speech-recognition": "^2.0.0",
"discord.js": "^13.1.0"
}
}
5 changes: 5 additions & 0 deletions examples/slashCommands/config.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"token": "Your bot token",
"guildId": "Guild where you have your bot",
"clientId": "Application id"
}
20 changes: 20 additions & 0 deletions examples/slashCommands/deployCommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const { clientId, guildId, token } = require("./config.json");

const commands = [
new SlashCommandBuilder()
.setName("join")
.setDescription("Bot joins voice channel and starts speech recognition"),
new SlashCommandBuilder()
.setName("leave")
.setDescription("Bot leaves voice channel"),
].map((command) => command.toJSON());

const rest = new REST({ version: "9" }).setToken(token);

rest
.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
.then(() => console.log("Successfully registered application commands."))
.catch(console.error);
53 changes: 53 additions & 0 deletions examples/slashCommands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { joinVoiceChannel, getVoiceConnection } = require("@discordjs/voice");
const { Client, Intents, GuildMember } = require("discord.js");
const { addSpeechEvent } = require("discord-speech-recognition");
const { token } = require("./config.json");

const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES],
});
addSpeechEvent(client);

client.on("ready", () => {
console.log("Ready!");
});

client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;

const { commandName } = interaction;

if (commandName === "join") {
if (
interaction.member instanceof GuildMember &&
interaction.member.voice.channel
) {
const channel = interaction.member.voice.channel;
connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
selfDeaf: false,
selfMute: true,
adapterCreator: channel.guild.voiceAdapterCreator,
});
await interaction.reply("Joined voice channel");
} else {
await interaction.reply("Join a voice channel and then try that again!");
}
}
if (commandName === "leave") {
const connection = getVoiceConnection(interaction.guildId);
if (connection) {
connection.destroy();
await interaction.reply("Left voice channel");
} else {
await interaction.reply("Bot isn't in voice channel!");
}
}
});

client.on("speech", (msg) => {
msg.author.send(msg.content);
});

client.login(token);
19 changes: 19 additions & 0 deletions examples/slashCommands/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "slash-commands",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@discordjs/builders": "^0.6.0",
"@discordjs/rest": "^0.1.0-canary.0",
"discord-api-types": "^0.23.1",
"discord-speech-recognition": "^2.0.0",
"discord.js": "^13.1.0"
}
}