Discord 机器人错误 - SequelizeDatabaseError: SQLITE_ERROR: no such table: users

Posted

技术标签:

【中文标题】Discord 机器人错误 - SequelizeDatabaseError: SQLITE_ERROR: no such table: users【英文标题】:Discord bot error - SequelizeDatabaseError: SQLITE_ERROR: no such table: users 【发布时间】:2020-11-08 22:26:43 【问题描述】:

我正在使用 sequelize 和 sqlite 在 dicord.j 沙子中制作一个不和谐机器人来制作数据库。它有一个货币系统,当有人在特定频道发帖并且我收到此错误SequelizeDatabaseError: SQLITE_ERROR: no such table: users 尽管在错误中它没有给出我哪里出错的线索这里是 main.js 文件:

const client = new Discord.Client();
const  Users, CurrencyShop  = require('F:/Users/User/Desktop/SuccessBot/dbObjects.js');
const  Op  = require('sequelize');
const async = require('async')
const currency = new Discord.Collection();
const token = ''
var CHANNEL_ID = '733612021869838387';
var LEADERBOARD_ID = '734352105481044019'
 
const prefix = '-';

Reflect.defineProperty(currency, 'add', 
    value: async function add(id, amount) 
        const user = currency.get(id);
        if (user) 
            user.balance += Number(amount);
            return user.save();
        
        const newUser = await Users.create( user_id: id, balance: amount );
        currency.set(id, newUser);
        return newUser;
    ,
);

Reflect.defineProperty(currency, 'getBalance', 
    value: function getBalance(id) 
        const user = currency.get(id);
        return user ? user.balance : 0;
    ,
);

client.once('ready', async () => 
    const storedBalances = await Users.findAll();
    storedBalances.forEach(b => currency.set(b.user_id, b));
    console.log('Radar Success is online!');
);
 
client.on('message', message =>
    if(message.author.bot) return;
    currency.add(message.author.id, 1);

    if (message.channel.id === CHANNEL_ID) 

        memberTag = message.member.id
        var score = '1'

        const target = message.mentions.users.first() || message.author;
        //return message.channel.send(`$target.tag has $currency.getBalance(target.id)????`);
        return message.channel.send('<@' + memberTag + `> Thanks for posting success! Your score is now $currency.getBalance(target.id)`);
    

    if (message.channel.id === LEADERBOARD_ID) 
        return message.channel.send(
            currency.sort((a, b) => b.balance - a.balance)
                .filter(user => client.users.cache.has(user.user_id))
                .first(10)
                .map((user, position) => `($position + 1) $(client.users.cache.get(user.user_id).tag): $user.balance????`)
                .join('\n'),
             code: true 
        );
    
 

);
 
client.login(token);

然后我有一个文件来存储不同的用户,存储他们的项目并存储商店: 用户.js

module.exports = (sequelize, DataTypes) => 
    return sequelize.define('users', 
        user_id: 
            type: DataTypes.STRING,
            primaryKey: true,
        ,
        balance: 
            type: DataTypes.INTEGER,
            defaultValue: 0,
            allowNull: false,
        ,
    , 
        timestamps: false,
    );
;

UserItems.js:

    return sequelize.define('user_item', 
        user_id: DataTypes.STRING,
        item_id: DataTypes.STRING,
        amount: 
            type: DataTypes.INTEGER,
            allowNull: false,
            'default': 0,
        ,
    , 
        timestamps: false,
    );
;

CurrencyShop.js

    return sequelize.define('currency_shop', 
        name: 
            type: DataTypes.STRING,
            unique: true,
        ,
        cost: 
            type: DataTypes.INTEGER,
            allowNull: false,
        ,
    , 
        timestamps: false,
    );
;

然后我使用 dbInit.js 设置 sequelize 数据库:



const sequelize = new Sequelize('database', 'username', 'password', 
    host: 'localhost',
    dialect: 'sqlite',
    logging: false,
    storage: 'database.sqlite',
);

const CurrencyShop = sequelize.import('models/CurrencyShop');
sequelize.import('models/Users');
sequelize.import('models/UserItems');

const force = process.argv.includes('--force') || process.argv.includes('-f');

sequelize.sync( force ).then(async () => 
    const shop = [
        CurrencyShop.upsert( name: 'Tea', cost: 1 ),
        CurrencyShop.upsert( name: 'Coffee', cost: 2 ),
        CurrencyShop.upsert( name: 'Cake', cost: 5 ),
    ];
    await Promise.all(shop);
    console.log('Database synced');
    sequelize.close();
).catch(console.error);

和 dbObjects.js:



const sequelize = new Sequelize('database', 'username', 'password', 
    host: 'localhost',
    dialect: 'sqlite',
    logging: false,
    storage: 'database.sqlite',
);

const Users = sequelize.import('./models/Users');
const CurrencyShop = sequelize.import('./models/CurrencyShop');
const UserItems = sequelize.import('./models/UserItems');

UserItems.belongsTo(CurrencyShop,  foreignKey: 'item_id', as: 'item' );

Users.prototype.addItem = async function(item) 
    const useritem = await UserItems.findOne(
        where:  user_id: this.user_id, item_id: item.id ,
    );

    if (useritem) 
        useritem.amount += 1;
        return useritem.save();
    

    return UserItems.create( user_id: this.user_id, item_id: item.id, amount: 1 );
;

Users.prototype.getItems = function() 
    return UserItems.findAll(
        where:  user_id: this.user_id ,
        include: ['item'],
    );
;

module.exports =  Users, CurrencyShop, UserItems ;

我知道这是一个非常广泛的问题,因为我已经提供了我所有的代码,但是如果有人在这方面非常有经验,这可能是一个快速的解决方案,期待答案!

编辑:我把我的机器人令牌留在那里,有人毁了我的服务器,谢谢大家爱你们,我猜是我的错。仍将不胜感激

【问题讨论】:

【参考方案1】:

这是一个 SQL 错误,您的数据库没有users

【讨论】:

【参考方案2】:

我猜你没有初始化数据库?

执行node dbInit.js创建数据库表。

https://discordjs.guide/sequelize/currency.html#initialize-database

【讨论】:

以上是关于Discord 机器人错误 - SequelizeDatabaseError: SQLITE_ERROR: no such table: users的主要内容,如果未能解决你的问题,请参考以下文章

当我使用节点启动机器人时,为啥 discord.js 模块出现错误?

如何使用 JS、Node.JS 和 Discord.JS 获取 SQLITE 数据库中的行数?

如何阻止我的 Discord 机器人调用错误的命令?

Discord.js 机器人 | Heroku 部署错误 | npm 错误!缺少脚本:开始

故障上托管的不和谐机器人错误:找不到模块'discord.js'

Discord 机器人类型错误。无法连接到我的服务器