Music Bot [Aoi.js]
This will show you how to create own Music Bot in aoi.js with basic stuff!
Package | Install | Required? |
---|---|---|
@aoijs/aoi.music | npm i @aoijs/aoi.music | true |
ffmpeg-static | npm i ffmpeg-static | true |
Code
index.js - Variables
client.variables({ requestedmusic: "", requester: ""})
variables.js - Variables
Requires require("./variables.js")(client);
in index.js
module.exports = (client) => { client.variables({ requestedmusic: "", requester: "" }, "main"); };
index.js
// Setup from https://aoi.js.org/
const { AoiClient, LoadCommands } = require("aoi.js");const { AoiVoice, PlayerEvents, PluginName, Cacher, Filter } = require("@akarui/aoi.music");
const client = new AoiClient({ token: "", prefix: "!", intents: ["MessageContent", "Guilds", "GuildMessages", "GuildVoiceStates"], events: ["onMessage", "onInteractionCreate"], database: { type: "aoi.db", db: require("@aoijs/aoi.db"), dbType: "KeyValue", tables: ["main"], securityKey: "a-32-characters-long-string-here" }});
client.loadCommands("./commands");
const voice = new AoiVoice(client, { requestOptions: { offsetTimeout: 0, soundcloudLikeTrackLimit: 200, }, searchOptions: { youtubeClient: "ANDROID" } });
client.variables({ requestedmusic: "", requester: ""})
client.command({ name: "ping", code: `Pong! $pingms`,});
voice.addPlugin(PluginName.Cacher, new Cacher("memory" /* or "disk" */));voice.addPlugin(PluginName.Filter, new Filter({ filterFromStart: false})),
voice.bindExecutor(client.functionManager.interpreter);
const loader = new LoadCommands(client);loader.load(voice.cmds, "./commands/musicevents/");
voice.addEvent(PlayerEvents.TrackStart)voice.addEvent(PlayerEvents.TrackEnd)
// ---------------------------------------------------------------------------------
module.exports = [{ channel: "$channelID", type: "eventName", code: `code to execute here`,}];
// -----------------------------------------------------------
client.status({ name: "https://youtube.com/@JosipFX", type: "WATCHING", time: 15,});
client.status({ name: "https://dsc.gg/arcwisedomain", type: "WATCHING", time: 15,});
play.js
module.exports =[{ name: "play", $if: "old", code: ` $if[$voiceID==] $description[You are not connected to the voice channel.] $color[Red]
$else
$if[$voiceID!=] $description[Where do you want me to search?] $color[Orange]
$addButton[1;Spotify;success;spotify;false] $addButton[1;Youtube;danger;youtube;false]
$setUserVar[requestedmusic;$message] $setChannelVar[requester;$authorID]
$endif $endif`},{ name: "youtube", type: 'interaction', prototype: 'button', code: ` $description[Searching...] $color[Orange]
$playTrack[$getUserVar[requestedmusic];youtube] $joinVC`},{ name: "spotify", type: 'interaction', prototype: 'button', code: ` $description[Searching...] $color[Orange]
$playTrack[$getUserVar[requestedmusic];spotify] $joinVC`}]
trackStart.js
module.exports = [{ channel: "$channelID", type: "trackStart", code: ` $title[Playing $songInfo[title]] $description[ *Artist* - $songInfo[artist] *Duration* - $humanizeMS[$songInfo[duration]] *Requester* - <@$getChannelVar[requester]>]
$color[Orange] $addTimestamp $footer[Made by @JosipFX]`}];
// You can add thumbnail of video but i wont add it, here is code anyway: $image[$songInfo[thumbnail]]
trackEnd.js
module.exports = [{ channel: "$channelID", type: "trackStart", code: ` $title[Playing $songInfo[title]] $description[ *Artist* - $songInfo[artist] *Duration* - $humanizeMS[$songInfo[duration]] *Requester* - <@$getChannelVar[requester]>]
$color[Orange] $addTimestamp $footer[Made by @JosipFX]`}];
// You can add thumbnail of video but i wont add it, here is code anyway: $image[$songInfo[thumbnail]]
Explanation
- This section will try its best to explain how each code works above, things you should know.
Index
How does it work?
- Nothing much its pretty simple, we just store aoi.music setup in here, as aoi.music keeps updating, you should follow whenever there is a breaking change because you may need to change client from “WEB” -> “ANDROID” example.
Play
How do I execute it?
[prefix]play [music]
How does it work?
- First we use old $if, so we can make it easier for ourselves. Then we use 2 $if’s which both check if you are in voice, if you are not it will return error, if you are in voice it will continue process. After that it asks you where to search Spotify or Youtube
- It also sets 2 new Values for Variables “requestedmusic”, “requester”. Requested music saves your song, and requester saves your User ID.
Youtube & Spotify Button
How does it work?
- This code gets triggered when you click Youtube / Spotify button. If you selected Spotify it will play Spotify, if you selected Youtube its gonna search on Youtube.
Track Start
How do I execute it?
- You don’t execute it, its an Event, it gets triggered when Music starts playing.
How does it work?
- When Music starts playing, this Event will get triggered, it will return information about Song that is playing right now. You can play around with it till you configure it to your likings
Function:
- $songInfo - More about it on Documentation
Track End
How do I execute it?
- You don’t execute it, its an Event, it gets triggered when Music ends.
How does it work?
- When Music ends, it will say “No more songs to play, leaving in 5s”, we use $wait[5s] to make it wait 5s, then
$leaveVC
to make Bot Leave Voice.
End
- Thanks to anyone who is still Following me on Youtube Channel and actually uses codes I make, appreciate that!
- Hope you liked this Music Bot in Aoi.js!
Credits
JosipFX - Made Code & Video