Skip to content

Commit

Permalink
feat(Twitch-Notifications): Added live-Roles (#81)
Browse files Browse the repository at this point in the history
* feat(Twitch-Notifications): Added live-Roles

* feat(twitch-notifications): Improved the live-roles

* feat(twitch-notifications): Switched to new type

Co-authored-by: Simon <simon.csaba@gmx.de>

* fix(Twitch-Notifications): Fixed an Error

Co-authored-by: Simon <simon.csaba@gmx.de>
  • Loading branch information
jateute and SCDerox authored Oct 12, 2022
1 parent 4590f58 commit 68918f8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
3 changes: 2 additions & 1 deletion locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@
},
"twitch-notifications": {
"channel-not-found": "Der Kanal mit der ID %c konnte nicht gefunden werden",
"user-not-on-twitch": "Nutzer %u konnte auf Twitch nicht gefunden werden"
"user-not-on-twitch": "Nutzer %u konnte auf Twitch nicht gefunden werden",
"user-not-found": "Der Benutzer mit der ID %u konnte nicht gefunden werden"
},
"fun": {
"slap-command-description": "Schlägt einen Nutzer ins Gesicht",
Expand Down
3 changes: 2 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@
},
"twitch-notifications": {
"channel-not-found": "Channel with ID %c could not be found",
"user-not-on-twitch": "Could not find user %u on twitch"
"user-not-on-twitch": "Could not find user %u on twitch",
"user-not-found": "Could not find the user with the id %u"
},
"hunt-the-code": {
"admin-command-description": "Manage the current Code-Hunt",
Expand Down
30 changes: 30 additions & 0 deletions modules/twitch-notifications/configs/streamers.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,36 @@
"type": "string",
"description-en": "Streamer where a notification should send when they start streaming",
"description-de": "Steamer, bei denen eine Benachrichtigung gesendet werden soll, wenn sie anfangen, zu streamen"
},
{
"field_name": "liveRole",
"humanname-en": "Use Live-Role",
"humanname-de": "Live-Rolle Aktivieren",
"default": false,
"type": "boolean",
"description-en": "Should the Live-Role be activated?",
"description-de": "Soll die Live-Rolle aktiviert sein?"
},
{
"field_name": "id",
"humanname-en": "Discord-User ID",
"humanname-de": "Discord-Benutzer ID",
"default": "",
"type": "userID",
"description-en": "ID of the Discord-Account of the Streamer",
"description-de": "ID des Discord-Accounts des Streamers",
"dependsOn": "liveRole"
},
{
"field_name": "role",
"humanname-en": "Live Role",
"humanname-de": "Live Rolle",
"default": "",
"type": "roleID",
"description-en": "ID of the Role that the Streamer should get, when live",
"description-de": "ID der Rolle, die der streamer bekommen soll, wenn er live ist",
"allowNull": true,
"dependsOn": "liveRole"
}
]
}
32 changes: 32 additions & 0 deletions modules/twitch-notifications/events/botReady.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ const {localize} = require('../../../src/functions/localize');
function twitchNotifications(client, apiClient) {
const streamers = client.configurations['twitch-notifications']['streamers'];

/**
* Function to add the Live-Role
* @param {string} userID ID of the User
* @param {String} roleID ID of the Role
* @param {boolean} liveRole Should the live-role be active
*/
async function addLiveRole(userID, roleID, liveRole) {
if (!liveRole) return;
if (!userID || userID === '' || !roleID || roleID === '') return;
await client.guild.members.fetch();
const member = client.guild.members.cache.get(userID);
if (!member) {
client.logger.error(localize('twitch-notifications', 'user-not-found', {u: userID}));
return;
}
await member.roles.add(roleID);
}

/**
* Sends the live-message
* @param {string} username Username of the streamer
Expand Down Expand Up @@ -75,10 +93,24 @@ function twitchNotifications(client, apiClient) {
startedAt: stream.startDate.toString()
});
sendMsg(stream.userDisplayName, stream.gameName, stream.thumbnailUrl, streamers[index]['liveMessageChannel'], stream.title, index);
addLiveRole(streamers[index]['id'], streamers[index]['role'], streamers[index]['liveRole']);
} else if (stream !== null && stream.startDate.toString() !== streamer.startedAt) {
streamer.startedAt = stream.startDate.toString();
streamer.save();
sendMsg(stream.userDisplayName, stream.gameName, stream.thumbnailUrl, streamers[index]['liveMessageChannel'], stream.title, index);
addLiveRole(streamers[index]['id'], streamers[index]['role'], streamers[index]['liveRole']);
} else if (stream === null) {
if (!streamers[index]['liveRole']) return;
if (!streamers[index]['id'] || streamers[index]['id'] === '' || !streamers[index]['role'] || streamers[index]['role'] === '') return;
await client.guild.members.fetch();
const member = client.guild.members.cache.get(streamers[index]['id']);
if (!member) {
client.logger.error(localize('twitch-notifications', 'user-not-found', {u: streamers[index]['id']}));
return;
}
if (member.roles.cache.has(streamers[index]['role'])) {
await member.roles.remove(streamers[index]['role']);
}
}
}
}
Expand Down

0 comments on commit 68918f8

Please sign in to comment.