【问题标题】:Build a Discord bot that Tweets构建一个发推文的 Discord 机器人
【发布时间】:2021-07-14 21:25:07
【问题描述】:

我的任务是创建一个机器人,当有人在 Discord 服务器上发帖时,它可以创建一条推文。除了 Twitter -> Discord 机器人之外,我找不到任何文档。但这显然是可行的,因为网站 https://www.Zapier.com 似乎提供了这一点。

到目前为止,我一直在为我的项目使用 discord.js 和 node.js。

【问题讨论】:

    标签: javascript twitter discord bots


    【解决方案1】:

    我想通了。

    // Discord Modules
    const Discord = require('discord.js')
    const fs = require('fs');
    const { prefix, token } = require('./config.js')
    
    // Express for the Heroku fix
    const express = require('express');
    const app = express();
    
    // Hook up to the server as a user
    const client = new Discord.Client()
    client.commands = new Discord.Collection()
    
    // Create a collection of available commands
    const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
    
    for (const file of commandFiles) {
        const command = require(`./commands/${file}`);
    
        // set a new item in the Collection
        // with the key as the command name and the value as the exported module
        client.commands.set(command.name, command);
    }
    
    
    // Listen for incoming chat commands, and compare them to the collection.
    client.on('message', async message => {
      if (!message.content.startsWith(prefix) || message.author.bot) return;
    
      const args = message.content.slice(prefix.length).split(/ +/);
      const commandName = args.shift().toLowerCase();
      const command = client.commands.get(commandName);
    
      if (!client.commands.has(commandName)) return;
      
      if (command.args && !args.length) {
        return message.channel.reply(`You didn't provide any arguments`)
      }
    
      try {
        command.execute(message, args)
      } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
      }
    })
    
    
      //For avoidong Heroku $PORT error
    app.set('port', (process.env.PORT || 5000));
    
    app.get('/', function(request, response) {
        var result = 'App is running'
        response.send(result);
    }).listen(app.get('port'), function() {
        console.log('App is running, server is listening on port ', app.get('port'));
    });
    
    client.login(token)
    

    以及为了发布推文而调用的命令

    // Twitter Modules
    const Twit = require('twit');
    const config = require('../config');
    const T = new Twit(config);
    
    module.exports = {
      name: 'tweet',
      description: 'create a tweet from a discord post',
      execute(message, args) {
        let discPost = args
        T.post('statuses/update', { status: discPost }, tweeted)
      }
    }
    
    // Make sure it worked!
    function tweeted (err, reply) {
      if (err !== undefined) {
        console.log(err)
      } else {
        console.log('Tweeted: ' + reply)
      }
    }
    

    丑陋的概念证明,但它确实有效。

    【讨论】:

    • 感谢您回答困扰我数周的您自己的问题。我正在尝试构建一个 IoT 项目,并想了解通知的工作原理。我的经验与您的相似,只能找到向 Discord 发送消息的解决方案,反之则不行。我会试一试,再次感谢。
    • 没问题!请注意,我在配置文件中获得了用于不和谐和 twitter 的 0Auth 令牌,仅此而已。
    猜你喜欢
    • 1970-01-01
    • 2021-04-26
    • 2021-09-09
    • 2018-08-22
    • 2019-02-19
    • 2021-08-19
    • 2021-07-10
    • 2022-11-12
    • 2021-06-18
    相关资源
    最近更新 更多