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

feat(Twitch-Notifications): Added live-Roles #81

Merged
merged 6 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
} 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']);
} 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