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

Added command that gets data from notion #2

Open
wants to merge 6 commits into
base: discord-bot
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
33 changes: 23 additions & 10 deletions commands/utility/addRole.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
const { SlashCommandBuilder } = require('discord.js');

const data = new SlashCommandBuilder()
.setName('ban')
.setDescription('Select a member and ban them.')
.addUserOption(option =>
option
.setName('target')
.setDescription('The member to ban')
.setRequired(true))
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles);
module.exports = {
data: new SlashCommandBuilder()
.setName('giverole')
.setDescription('Gives a user a role')
.addRoleOption(option =>
option.setName('role')
.setDescription('Role to give')
.setRequired(true))
.addUserOption(option => option.setName('user').setDescription('User to give role to').setRequired(true)),
async execute(interaction) {
const role = interaction.options.getRole('role');
const user = interaction.options.getMember('user');
const guild = interaction.guild;
if (!role) {
await interaction.reply(`Role not found.`);
return;
}
await user.roles.add(role);

await interaction.reply(`Role '${role.name}' has been given to '${user}'.`);
},
};
17 changes: 17 additions & 0 deletions commands/utility/notiondata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// const { SlashCommandBuilder } = require('discord.js');
// //require('dotenv').config()
// const {notion_page_id, notion_api_key} = require("../../config.json")
// const {fetchNotionDatabase, parseResults}= require("../../services/notionFetch")
// const { Client } = require("@notionhq/client")
// const notion = new Client({ auth: notion_api_key })


// module.exports = {
// data: new SlashCommandBuilder()
// .setName('notiondata')
// .setDescription('Lists some members from the notion'),
// async execute(interaction) {
// let ndata = await fetchNotionDatabase(notion, notion_page_id)
// await interaction.reply(parseResults(ndata.results));
// },
// };
10 changes: 0 additions & 10 deletions commands/utility/ping.js

This file was deleted.

11 changes: 0 additions & 11 deletions commands/utility/server.js

This file was deleted.

31 changes: 31 additions & 0 deletions commands/utility/setRole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { SlashCommandBuilder } = require('discord.js');
const {fetchNotionDatabase, parseResults,collectHandles}= require("../../services/notionFetch")
const {notion_page_id, notion_api_key} = require("../../config.json")

const {Client} = require("@notionhq/client");
const notion = new Client({ auth: notion_api_key })

module.exports = {
data: new SlashCommandBuilder()
.setName('setrole')
.setDescription('Gives a user a role based on notion data')
.addUserOption(option => option.setName('user').setDescription('User to give role to').setRequired(true)),
async execute(interaction) {
//const role = interaction.options.getRole('role');
const user = interaction.options.getMember('user');
const guild = interaction.guild;
const username = user.user.username

let ndata = await fetchNotionDatabase(notion, notion_page_id)
let roledata = await collectHandles(ndata.results)
const role = guild.roles.cache.find(role => role.name === roledata[username][1]);
let membername = roledata[username][0]
if (!role) {
await interaction.reply(`Role not found.`);
return;
}
await user.roles.add(role);

await interaction.reply(`${username} (${membername }) has been assigned to ${role}`);
},
};
12 changes: 0 additions & 12 deletions commands/utility/user.js

This file was deleted.

72 changes: 72 additions & 0 deletions services/notionFetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
async function fetchNotionDatabase(notionClient, databaseId) {
//get data from notion as a json respons to api query
let returnResponse = {"results": []}
try {
let response = await notionClient.databases.query({
database_id: databaseId,
});
returnResponse.results.push(...response.results)
while (response.has_more){
response = await notionClient.databases.query({
database_id: databaseId,
start_cursor: response.next_cursor
});
returnResponse.results.push(...response.results)
}

return returnResponse;
} catch (error) {
console.error(error.body);
throw new Error('Failed to fetch Notion database');
}
}

function collectHandles(res){
let handles = {}
let handleRichText = ""
for (row of res){
handleRichText = row.properties["Discord Handle"].rich_text
if (handleRichText.length > 0) {
handles[handleRichText[0].plain_text] = [row.properties.Name.title[0].plain_text,
row.properties['Current Role'].select.name]

}


}
return handles
}

function parseResults(res){
//Select 10 random members from the database and print name + position
let name = "";
let pos = "";
let discHandle = "";
let discHandleRichText = "";

const numMembers = res.length;

let output = `There are ${numMembers} members in the directory\n`;
output = output.concat(`Here are 10 random members:\n`)

let rowInd = 0;
for (let i = 0; i < 10; i++) {
rowInd = Math.floor(Math.random()*numMembers)
name = res[rowInd].properties.Name.title[0].plain_text;
pos = res[rowInd].properties['Current Role'].select.name;
discHandleRichText = res[rowInd].properties["Discord Handle"].rich_text
if (discHandleRichText.length > 0) {
discHandle = discHandleRichText[0].plain_text
} else {
discHandle = "No Discord Handle Found"
}
output = output.concat(`${name} (${pos}) (${discHandle})\n`)
}
return output
}

module.exports = {
fetchNotionDatabase,
parseResults,
collectHandles
}