Skip to content

Commit 3cf7156

Browse files
authored
feat: discord bots (LAION-AI#1953)
After a lot of prs this pr only includes the discord bots, moved both discord bot to same folder.
1 parent 80ffab1 commit 3cf7156

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2064
-0
lines changed

discord-bots/oa-bot-js/.env.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
TOKEN=Discord bot token
2+
CLIENT_ID=Discord bot id
3+
OA_APIKEY=OpenAssistant API key
4+
OA_APIURL=OpenAssistant API url

discord-bots/oa-bot-js/.gitignore

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
chatgpt-io/
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
db.json
85+
86+
# Gatsby files
87+
.cache/
88+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
89+
# https://nextjs.org/blog/next-9-1#public-directory-support
90+
# public
91+
92+
# vuepress build output
93+
.vuepress/dist
94+
95+
# Serverless directories
96+
.serverless/
97+
98+
# FuseBox cache
99+
.fusebox/
100+
101+
# DynamoDB Local files
102+
.dynamodb/
103+
104+
# TernJS port file
105+
.tern-port
106+
107+
# Private files
108+
token-handling.js
109+
package-lock.json

discord-bots/oa-bot-js/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Open Assistant Discord bot
2+
3+
This is a bot for the Open Assistant project. It is a Discord bot that allows
4+
you to interact with Open Assistant.
5+
6+
## Start bot
7+
8+
1. Install the dependencies with `npm install`
9+
2. Change .env.sample to .env and fill in the values
10+
3. Run the bot with `npm start` for development mode remember to restart the bot
11+
after every change.

discord-bots/oa-bot-js/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "open-assistant-discord",
3+
"description": "This is a bot for the Open Assistant project. It is a Discord bot that allows you to interact with Open Assistant.",
4+
"version": "1.0.0",
5+
"main": "dist/index.js",
6+
"type": "module",
7+
"scripts": {
8+
"build": "tsc",
9+
"start": "tsc && node .",
10+
"dev": "concurrently \"tsc --watch\" \"npx nodemon\""
11+
},
12+
"author": "MrlolDev",
13+
"license": "Apache-2.0",
14+
"private": true,
15+
"dependencies": {
16+
"chalk": "^5.2.0",
17+
"discord.js": "^14.7.1",
18+
"dotenv": "^16.0.3",
19+
"open-assistant.js": "^1.0.4"
20+
},
21+
"devDependencies": {
22+
"@types/node": "^18.14.1",
23+
"concurrently": "^7.6.0",
24+
"nodemon": "^2.0.20",
25+
"typescript": "^4.9.5"
26+
}
27+
}

discord-bots/oa-bot-js/src/bot.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Require the necessary discord.js classes
2+
import {
3+
Client,
4+
Collection,
5+
GatewayIntentBits,
6+
REST,
7+
Partials,
8+
} from "discord.js";
9+
import "dotenv/config";
10+
import eventHandler from "./handlers/events.js";
11+
import commandHandler from "./handlers/commands.js";
12+
import interactionsHandler from "./handlers/interactions.js";
13+
14+
// Create a new client instance
15+
const client: any = new Client({
16+
intents: [
17+
GatewayIntentBits.Guilds,
18+
GatewayIntentBits.GuildMessages,
19+
GatewayIntentBits.GuildMessageReactions,
20+
GatewayIntentBits.DirectMessages,
21+
GatewayIntentBits.GuildVoiceStates,
22+
],
23+
partials: [
24+
Partials.User, // We want to receive uncached users!
25+
Partials.Channel,
26+
Partials.Message,
27+
],
28+
});
29+
const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);
30+
31+
client.commands = new Collection();
32+
client.interactions = new Collection();
33+
client.tasks = [];
34+
client.version = "1.0.0";
35+
36+
// Handlers
37+
eventHandler(client);
38+
commandHandler(client);
39+
interactionsHandler(client);
40+
// Log in to Discord with your client's token
41+
client.login(process.env.TOKEN);
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import {
2+
ActionRowBuilder,
3+
EmbedBuilder,
4+
SlashCommandBuilder,
5+
ButtonBuilder,
6+
time,
7+
ButtonStyle,
8+
} from "discord.js";
9+
import path from "node:path";
10+
import { fileURLToPath } from "url";
11+
12+
export default {
13+
data: new SlashCommandBuilder()
14+
.setName("bot")
15+
.setDescription("Get the info of the bot"),
16+
async execute(interaction, client, commands, commandType) {
17+
const timeString = time(client.user.createdAt, "R");
18+
const __filename = fileURLToPath(import.meta.url);
19+
const __dirname = path.dirname(__filename);
20+
21+
var shard = client.shard.client.options.shards[0] + 1;
22+
23+
await commandType.load(interaction);
24+
var totalGuildsR = await client.shard.fetchClientValues(
25+
"guilds.cache.size"
26+
);
27+
const totalGuilds = totalGuildsR.reduce(
28+
(acc, guildCount) => acc + guildCount,
29+
0
30+
);
31+
var totalMembersR = await client.shard.broadcastEval((c) =>
32+
c.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0)
33+
);
34+
const totalMembers = totalMembersR.reduce(
35+
(acc, memberCount) => acc + memberCount,
36+
0
37+
);
38+
39+
var embed = new EmbedBuilder()
40+
.setColor("#3a82f7")
41+
.setTimestamp()
42+
.setURL("https://open-assistant.io")
43+
.setTitle("Open Assistant")
44+
.addFields([
45+
{
46+
name: "Ping",
47+
value: `🏓Latency is ${
48+
Date.now() - interaction.createdTimestamp
49+
}ms. API Latency is ${Math.round(client.ws.ping)}ms.`,
50+
inline: true,
51+
},
52+
{
53+
name: "Servers",
54+
value: `${totalGuilds}`,
55+
inline: true,
56+
},
57+
{
58+
name: "Users",
59+
value: `${totalMembers}`,
60+
inline: true,
61+
},
62+
{
63+
name: "Created At",
64+
value: `${timeString}`,
65+
inline: true,
66+
},
67+
{
68+
name: "Library",
69+
value: "Discord.js",
70+
inline: true,
71+
},
72+
{
73+
name: "Shard",
74+
value: `${shard}`,
75+
inline: true,
76+
},
77+
{
78+
name: "RAM Usage",
79+
value: `${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(
80+
2
81+
)} MB`,
82+
inline: true,
83+
},
84+
{
85+
name: "Version",
86+
value: `v${client.version}`,
87+
inline: true,
88+
},
89+
]);
90+
91+
const row = new ActionRowBuilder().addComponents(
92+
new ButtonBuilder()
93+
.setLabel("Add me")
94+
.setURL(
95+
`https://discord.com/api/oauth2/authorize?client_id=${client.id}&permissions=281357371712&scope=bot%20applications.commands`
96+
)
97+
.setStyle(ButtonStyle.Link),
98+
new ButtonBuilder()
99+
.setLabel("Support server")
100+
.setURL("https://discord.com/invite/H769HxZyb5")
101+
.setStyle(ButtonStyle.Link),
102+
new ButtonBuilder()
103+
.setLabel("Github Repo")
104+
.setURL("https://github.com/LAION-AI/Open-Assistant")
105+
.setStyle(ButtonStyle.Link)
106+
);
107+
await commandType.reply(interaction, {
108+
embeds: [embed],
109+
components: [row],
110+
});
111+
return;
112+
},
113+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { SlashCommandBuilder } from "discord.js";
2+
import { getUserLang } from "../modules/open-assistant/user.js";
3+
import { getTranlation } from "../modules/open-assistant/langs.js";
4+
import {
5+
langInteraction,
6+
initInteraction,
7+
} from "../modules/open-assistant/interactions.js";
8+
9+
export default {
10+
data: new SlashCommandBuilder()
11+
.setName("open-assistant")
12+
.setDescription("Help in the data collection of open assistant"),
13+
async execute(interaction, client, commands, commandType) {
14+
var lang = await getUserLang(interaction.user.id);
15+
await interaction.deferReply();
16+
17+
if (!lang) {
18+
await langInteraction(interaction);
19+
} else {
20+
var translation = await getTranlation(lang);
21+
await initInteraction(interaction, translation, lang);
22+
return;
23+
}
24+
},
25+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Events } from "discord.js";
2+
3+
const interactionType = {
4+
type: "interaction",
5+
load: async (interaction) => {
6+
await interaction.deferReply();
7+
},
8+
reply: async (interaction, content) => {
9+
if (interaction.deferred || interaction.replied) {
10+
await interaction.editReply(content);
11+
} else {
12+
await interaction.reply(content);
13+
}
14+
},
15+
};
16+
17+
export default {
18+
name: Events.InteractionCreate,
19+
once: false,
20+
async execute(interaction, client) {
21+
if (
22+
!interaction.isChatInputCommand() &&
23+
!interaction.isContextMenuCommand()
24+
)
25+
return;
26+
var commands = await client.commands.toJSON();
27+
const command = interaction.client.commands.get(interaction.commandName);
28+
29+
if (!command) {
30+
console.error(
31+
`No command matching ${interaction.commandName} was found.`
32+
);
33+
return;
34+
}
35+
var guildId;
36+
if (interaction.guild) guildId = interaction.guild.id;
37+
38+
try {
39+
await command.execute(interaction, client, commands, interactionType);
40+
} catch (error) {
41+
console.log(error);
42+
await interactionType.reply(interaction, {
43+
content: "There was an error while executing this command!",
44+
ephemeral: true,
45+
});
46+
}
47+
},
48+
};

0 commit comments

Comments
 (0)