Basic User and Guild registration
This commit is contained in:
32
commands/register.js
Normal file
32
commands/register.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
//User registration
|
||||||
|
const { SlashCommandBuilder } = require("@discordjs/builders");
|
||||||
|
const { User } = require("../models");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
data: new SlashCommandBuilder()
|
||||||
|
.setName("register")
|
||||||
|
.setDescription("Register yourself"),
|
||||||
|
async execute(interaction) {
|
||||||
|
let user = await User.findOne({
|
||||||
|
where: {
|
||||||
|
userID: interaction.user.id
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (user) {
|
||||||
|
interaction.reply({
|
||||||
|
content: "You are already registered",
|
||||||
|
ephemeral: true
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await User.create({
|
||||||
|
userID: interaction.user.id,
|
||||||
|
banned: false,
|
||||||
|
registredAt: new Date()
|
||||||
|
});
|
||||||
|
interaction.reply({
|
||||||
|
content: "You are now registered",
|
||||||
|
ephemeral: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
24
commands/userlist.js
Normal file
24
commands/userlist.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
const { SlashCommandBuilder } = require("@discordjs/builders");
|
||||||
|
const { User } = require("../models");
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
data: new SlashCommandBuilder()
|
||||||
|
.setName("userlist")
|
||||||
|
.setDescription("List all users"),
|
||||||
|
async execute(interaction) {
|
||||||
|
let users = await User.findAll();
|
||||||
|
let userList = "";
|
||||||
|
for (let i = 0; i < users.length; i++) {
|
||||||
|
let username = await interaction.client.users.fetch(users[i].userID);
|
||||||
|
userList += `${username.username}#${username.discriminator}`;
|
||||||
|
}
|
||||||
|
if (userList === "") {
|
||||||
|
userList = "No users found";
|
||||||
|
}
|
||||||
|
interaction.reply({
|
||||||
|
content: userList,
|
||||||
|
ephemeral: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,34 @@
|
|||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
const { REST } = require("@discordjs/rest");
|
const { REST } = require("@discordjs/rest");
|
||||||
const { Routes } = require("discord-api-types/v9")
|
const { Routes } = require("discord-api-types/v9")
|
||||||
|
const { Guild } = require("../models");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: "interactionCreate",
|
name: "interactionCreate",
|
||||||
async execute (interaction) {
|
async execute (interaction) {
|
||||||
if (!interaction.isCommand()) return;
|
if (!interaction.isCommand()) return;
|
||||||
|
|
||||||
|
const guild = await interaction.guild;
|
||||||
|
//check if guild exists in database
|
||||||
|
let guildData = await Guild.findOne({
|
||||||
|
where: {
|
||||||
|
guildID: guild.id
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!guildData) {
|
||||||
|
//create guild in database
|
||||||
|
await Guild.create({
|
||||||
|
guildID: guild.id,
|
||||||
|
ownerID: guild.ownerId,
|
||||||
|
});
|
||||||
|
//send guild registered message to the interations channel
|
||||||
|
interaction.channel.send({
|
||||||
|
content: `Guild ${guild.name} registered`,
|
||||||
|
ephemeral: false
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
const command = interaction.client.commands.get(interaction.commandName);
|
const command = interaction.client.commands.get(interaction.commandName);
|
||||||
|
|
||||||
if (!command) return;
|
if (!command) return;
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ module.exports = {
|
|||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
type: Sequelize.INTEGER
|
type: Sequelize.INTEGER
|
||||||
},
|
},
|
||||||
gid: {
|
guildID: {
|
||||||
type: Sequelize.STRING
|
type: Sequelize.STRING
|
||||||
},
|
},
|
||||||
adminRole: {
|
adminRoleID: {
|
||||||
type: Sequelize.STRING
|
type: Sequelize.STRING
|
||||||
},
|
},
|
||||||
owderId: {
|
owderID: {
|
||||||
type: Sequelize.STRING
|
type: Sequelize.STRING
|
||||||
},
|
},
|
||||||
createdAt: {
|
createdAt: {
|
||||||
|
|||||||
33
migrations/20220417132721-create-user.js
Normal file
33
migrations/20220417132721-create-user.js
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
'use strict';
|
||||||
|
module.exports = {
|
||||||
|
async up(queryInterface, Sequelize) {
|
||||||
|
await queryInterface.createTable('Users', {
|
||||||
|
id: {
|
||||||
|
allowNull: false,
|
||||||
|
autoIncrement: true,
|
||||||
|
primaryKey: true,
|
||||||
|
type: Sequelize.INTEGER
|
||||||
|
},
|
||||||
|
userID: {
|
||||||
|
type: Sequelize.STRING
|
||||||
|
},
|
||||||
|
banned: {
|
||||||
|
type: Sequelize.BOOLEAN
|
||||||
|
},
|
||||||
|
registredAt: {
|
||||||
|
type: Sequelize.DATE
|
||||||
|
},
|
||||||
|
createdAt: {
|
||||||
|
allowNull: false,
|
||||||
|
type: Sequelize.DATE
|
||||||
|
},
|
||||||
|
updatedAt: {
|
||||||
|
allowNull: false,
|
||||||
|
type: Sequelize.DATE
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
async down(queryInterface, Sequelize) {
|
||||||
|
await queryInterface.dropTable('Users');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -14,9 +14,9 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Guild.init({
|
Guild.init({
|
||||||
gid: DataTypes.STRING,
|
guildID: DataTypes.STRING,
|
||||||
adminRole: DataTypes.STRING,
|
adminRoleID: DataTypes.STRING,
|
||||||
owderId: DataTypes.STRING
|
owderID: DataTypes.STRING
|
||||||
}, {
|
}, {
|
||||||
sequelize,
|
sequelize,
|
||||||
modelName: 'Guild',
|
modelName: 'Guild',
|
||||||
|
|||||||
25
models/user.js
Normal file
25
models/user.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
'use strict';
|
||||||
|
const {
|
||||||
|
Model
|
||||||
|
} = require('sequelize');
|
||||||
|
module.exports = (sequelize, DataTypes) => {
|
||||||
|
class User extends Model {
|
||||||
|
/**
|
||||||
|
* Helper method for defining associations.
|
||||||
|
* This method is not a part of Sequelize lifecycle.
|
||||||
|
* The `models/index` file will call this method automatically.
|
||||||
|
*/
|
||||||
|
static associate(models) {
|
||||||
|
// define association here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
User.init({
|
||||||
|
userID: DataTypes.STRING,
|
||||||
|
banned: DataTypes.BOOLEAN,
|
||||||
|
registredAt: DataTypes.DATE
|
||||||
|
}, {
|
||||||
|
sequelize,
|
||||||
|
modelName: 'User',
|
||||||
|
});
|
||||||
|
return User;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user