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

Commit 456f213

Browse files
committed
Add queue system and improvements
This commits adds the new queue system and the new PR/issue template md file.
1 parent 7b02c7d commit 456f213

File tree

5 files changed

+520
-82
lines changed

5 files changed

+520
-82
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
name: Bug report
3+
about: Help the project be more stable reporting bugs
4+
title: '[bug]: '
5+
labels: bug
6+
assignees: ''
7+
---
8+
9+
## What's wrong?
10+
11+
Inform here what happened.
12+
13+
## Reproducing
14+
15+
Let us know how to reproduce this bug.
16+
17+
## Crashes (debug)
18+
19+
If the bot crashed, please debug it with gdb, following the example below:
20+
21+
```console
22+
$ gdb ./ConcordBot
23+
```
24+
25+
Then, press `c` and `enter`, and now that you are on GDB's shell, please use the command `run`.
26+
27+
You will need to reproduce the crash, and when you successfully reproduce it and crashes the bot, please use the command `bt` and send what you got from the response here.
28+
29+
## Screenshots
30+
31+
If possible, let a screenshot here, if not, please remove this section.
32+
33+
## Final
34+
35+
- [ ] I confirm that I've tried reproducing this bug on the latest release and I'm still able to reproduce

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Changes
2+
3+
Here explain what changes have you made in the bot.
4+
5+
## Why?
6+
7+
Why have you made this PR?
8+
9+
## Tested?
10+
11+
If you agree that you have tested all systems that your changes affected, please type "I agree that I tested all systems that my changes affected.": `Type here`
12+
13+
## Testing
14+
15+
Please explain here how you made the testing, please be as specific as possible.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ There are currently two ways of using it. Compiling the source code and using it
1111
* 1 - Use `git clone https://github.com/ThePedroo/C-Music-Discord-Bot.git` to get the bot files.
1212
* 2 - Install clang / GCC.
1313
* 3 - Install and build [Concord](https://github.com/Cogmasters/concord).
14-
* Info. Dont't have a [Lavalink](https://github.com/freyacodes/Lavalink) node? Setup it on [replit.com](https://replit.com/).
14+
* Info. Don't have a [Lavalink](https://github.com/freyacodes/Lavalink) node? Setup it on [replit.com](https://replit.com/).
1515
* 4 - Install sqlite3: `sudo apt install sqlite3`
1616

1717
If you did everything correct, use:
@@ -23,7 +23,7 @@ Done, now you will have a compiled version of the music bot for your architectur
2323

2424
## Pre-compiled
2525

26-
* 1 - Go to Github releases and download the `Concord-x86_64` file, after it run it.
26+
* 1 - Go to Github releases and download the pre-compiled file that matches your CPU architecute, after it runs it.
2727

2828
Done! Yeah, it's that simple to use it. ^^
2929

lavalink.c

Lines changed: 217 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ void on_cycle(struct discord *client) {
3232
}
3333

3434
void on_text(void *data, struct websockets *ws, struct ws_info *info, const char *text, size_t len) {
35-
(void) ws; (void) info;
35+
(void)ws; (void)info;
36+
printf("%s\n", text);
3637

3738
jsmn_parser parser;
3839
jsmntok_t tokens[1024];
@@ -106,39 +107,224 @@ void on_text(void *data, struct websockets *ws, struct ws_info *info, const char
106107
}
107108
return;
108109
} else {
109-
struct discord_embed embed[] = {
110-
{
111-
.description = "The music has ended.",
112-
.image =
113-
&(struct discord_embed_image){
114-
.url = "https://raw.githubusercontent.com/Cogmasters/concord/master/docs/static/social-preview.png",
115-
},
116-
.footer =
117-
&(struct discord_embed_footer){
118-
.text = "Powered by Concord",
119-
.icon_url = "https://raw.githubusercontent.com/Cogmasters/concord/master/docs/static/concord-small.png",
110+
long long channelID = sqlite3_column_int64(stmt, 0);
111+
112+
if (sqlite3_finalize(stmt) != SQLITE_OK) {
113+
log_fatal("[SQLITE] Error while executing function sqlite3_finalize.");
114+
return;
115+
}
116+
117+
query = sqlite3_mprintf("SELECT array FROM guild_queue WHERE guild_id = %.*s;", (int)guildId->v.len, text + guildId->v.pos);
118+
rc = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
119+
120+
sqlite3_free(query);
121+
122+
(rc = sqlite3_step(stmt));
123+
124+
log_warn("%s\n", sqlite3_column_text(stmt, 0));
125+
126+
if (rc == SQLITE_ROW) {
127+
char json[1024];
128+
snprintf(json, sizeof(json), "%s", sqlite3_column_text(stmt, 0));
129+
130+
jsmn_parser parser;
131+
jsmntok_t tokens[1024];
132+
133+
jsmn_init(&parser);
134+
int r = jsmn_parse(&parser, json, sizeof(json), tokens, sizeof(tokens));
135+
136+
if (r < 0) {
137+
log_error("[JSMNF] Failed to parse JSON.");
138+
return;
139+
}
140+
141+
jsmnf_loader loader;
142+
jsmnf_pair pairs[1024];
143+
144+
jsmnf_init(&loader);
145+
r = jsmnf_load(&loader, json, tokens, parser.toknext, pairs, 1024);
146+
147+
if (r < 0) {
148+
log_error("[JSMNF] Failed to load JSMNF.");
149+
return;
150+
}
151+
152+
if (sqlite3_finalize(stmt) != SQLITE_OK) {
153+
log_fatal("[SQLITE] Error while executing function sqlite3_finalize.");
154+
return;
155+
}
156+
157+
jsmnf_pair *nTrack = &pairs->fields[1];
158+
159+
char pJ[1024];
160+
snprintf(pJ, sizeof(pJ), "{\"op\":\"play\",\"guildId\":\"%.*s\",\"track\":\"%.*s\",\"noReplace\":false,\"pause\":false}", (int)guildId->v.len, text + guildId->v.pos, (int)nTrack->v.len, json + nTrack->v.pos);
161+
162+
sendPayload(pJ, "play");
163+
164+
jsonb b;
165+
char qbuf[1024];
166+
167+
jsonb_init(&b);
168+
jsonb_array(&b, qbuf, sizeof(qbuf));
169+
170+
if (pairs->size == 1) {
171+
char *msgErr = NULL;
172+
173+
query = sqlite3_mprintf("DELETE FROM guild_queue WHERE guild_id = %.*s;", (int)guildId->v.len, text + guildId->v.pos);
174+
rc = sqlite3_exec(db, query, NULL, NULL, &msgErr);
175+
176+
sqlite3_free(query);
177+
178+
if (rc != SQLITE_OK) {
179+
log_fatal("[SYSTEM] Something went wrong while deleting guild_queue table from guild_id. [%s]", msgErr);
180+
if (sqlite3_close(db) != SQLITE_OK) {
181+
log_fatal("[SQLITE] Failed to close sqlite db. [%s]", sqlite3_errmsg(db));
182+
}
183+
return;
184+
} else {
185+
log_debug("[SYSTEM] Deleted guild_queue where guild_id = %.*s.", (int)guildId->v.len, text + guildId->v.pos);
186+
}
187+
188+
if (sqlite3_close(db) != SQLITE_OK) {
189+
log_fatal("[SQLITE] Failed to close sqlite db. [%s]", sqlite3_errmsg(db));
190+
}
191+
192+
struct discord_embed embed[] = {
193+
{
194+
.description = "The music has ended.",
195+
.image =
196+
&(struct discord_embed_image){
197+
.url = "https://raw.githubusercontent.com/Cogmasters/concord/master/docs/static/social-preview.png",
198+
},
199+
.footer =
200+
&(struct discord_embed_footer){
201+
.text = "Powered by Concord",
202+
.icon_url = "https://raw.githubusercontent.com/Cogmasters/concord/master/docs/static/concord-small.png",
203+
},
204+
.timestamp = discord_timestamp(data),
205+
.color = 15615
206+
}
207+
};
208+
209+
struct discord_create_message params = {
210+
.flags = 0,
211+
.embeds =
212+
&(struct discord_embeds){
213+
.size = 1,
214+
.array = embed,
215+
},
216+
};
217+
218+
discord_create_message(data, channelID, &params, NULL);
219+
return;
220+
}
221+
222+
jsmnf_pair *f;
223+
224+
for (int i = 1; i < pairs->size; ++i) {
225+
f = &pairs->fields[i];
226+
char arrayTrack[256];
227+
snprintf(arrayTrack, sizeof(arrayTrack), "%.*s", (int)f->v.len, json + f->v.pos);
228+
jsonb_string(&b, qbuf, sizeof(qbuf), arrayTrack, strlen(arrayTrack));
229+
}
230+
231+
jsonb_array_pop(&b, qbuf, sizeof(qbuf));
232+
233+
char *msgErr = NULL;
234+
235+
char *query = sqlite3_mprintf("DELETE FROM guild_queue WHERE guild_id = %.*s;", (int)guildId->v.len, text + guildId->v.pos);
236+
rc = sqlite3_exec(db, query, NULL, NULL, &msgErr);
237+
238+
sqlite3_free(query);
239+
240+
if (rc != SQLITE_OK) {
241+
log_fatal("[SYSTEM] Something went wrong while deleting guild_queue table from guild_id. [%s]", msgErr);
242+
if (sqlite3_close(db) != SQLITE_OK) {
243+
log_fatal("[SQLITE] Failed to close sqlite db. [%s]", sqlite3_errmsg(db));
244+
}
245+
return;
246+
} else {
247+
log_debug("[SYSTEM] Deleted guild_queue where guild_id = %.*s.", (int)guildId->v.len, text + guildId->v.pos);
248+
}
249+
250+
query = sqlite3_mprintf("INSERT INTO guild_queue(guild_id, array) values(%.*s, '%s');", (int)guildId->v.len, text + guildId->v.pos, qbuf);
251+
rc = sqlite3_exec(db, query, NULL, NULL, &msgErr);
252+
253+
sqlite3_free(query);
254+
255+
if (rc != SQLITE_OK) {
256+
log_fatal("[SQLITE] Something went wrong while inserting values into guild_queue table. [%s]", msgErr);
257+
if (sqlite3_close(db) != SQLITE_OK) {
258+
log_fatal("[SQLITE] Failed to close sqlite db. [%s]", sqlite3_errmsg(db));
259+
}
260+
return;
261+
} else {
262+
log_debug("[SQLITE] Inserted into guild_queue table the values: guild_id: %.*s, array: %s.", (int)guildId->v.len, text + guildId->v.pos, qbuf);
263+
}
264+
265+
struct discord_embed embed[] = {
266+
{
267+
.description = "Now, going to play next music from queue.",
268+
.image =
269+
&(struct discord_embed_image){
270+
.url = "https://raw.githubusercontent.com/Cogmasters/concord/master/docs/static/social-preview.png",
271+
},
272+
.footer =
273+
&(struct discord_embed_footer){
274+
.text = "Powered by Concord",
275+
.icon_url = "https://raw.githubusercontent.com/Cogmasters/concord/master/docs/static/concord-small.png",
276+
},
277+
.timestamp = discord_timestamp(data),
278+
.color = 15615
279+
}
280+
};
281+
282+
struct discord_create_message params = {
283+
.flags = 0,
284+
.embeds =
285+
&(struct discord_embeds){
286+
.size = 1,
287+
.array = embed,
120288
},
121-
.timestamp = discord_timestamp(data),
122-
.color = 15615
289+
};
290+
291+
discord_create_message(data, channelID, &params, NULL);
292+
} else {
293+
if (sqlite3_finalize(stmt) != SQLITE_OK) {
294+
log_fatal("[SQLITE] Error while executing function sqlite3_finalize.");
295+
return;
123296
}
124-
};
125-
126-
struct discord_create_message params = {
127-
.flags = 0,
128-
.embeds =
129-
&(struct discord_embeds){
130-
.size = 1,
131-
.array = embed,
132-
},
133-
};
134-
135-
discord_create_message(data, sqlite3_column_int64(stmt, 0), &params, NULL);
136-
}
137-
138-
if (sqlite3_finalize(stmt) != SQLITE_OK) {
139-
log_fatal("[SQLITE] Error while executing function sqlite3_finalize.");
140-
return;
297+
298+
struct discord_embed embed[] = {
299+
{
300+
.description = "<a:Noo:757568484086382622> | Some wild error happened, this should be reported.",
301+
.image =
302+
&(struct discord_embed_image){
303+
.url = "https://raw.githubusercontent.com/Cogmasters/concord/master/docs/static/social-preview.png",
304+
},
305+
.footer =
306+
&(struct discord_embed_footer){
307+
.text = "Powered by Concord",
308+
.icon_url = "https://raw.githubusercontent.com/Cogmasters/concord/master/docs/static/concord-small.png",
309+
},
310+
.timestamp = discord_timestamp(data),
311+
.color = 16711680
312+
}
313+
};
314+
315+
struct discord_create_message params = {
316+
.flags = 0,
317+
.embeds =
318+
&(struct discord_embeds){
319+
.size = 1,
320+
.array = embed,
321+
},
322+
};
323+
324+
discord_create_message(data, channelID, &params, NULL);
325+
}
141326
}
327+
142328
if (sqlite3_close(db) != SQLITE_OK) {
143329
log_fatal("[SQLITE] Failed to close sqlite db. [%s]", sqlite3_errmsg(db));
144330
}

0 commit comments

Comments
 (0)