Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 构建一个不和谐的推特机器人_Javascript_Twitter_Discord_Bots - Fatal编程技术网

Javascript 构建一个不和谐的推特机器人

Javascript 构建一个不和谐的推特机器人,javascript,twitter,discord,bots,Javascript,Twitter,Discord,Bots,我的任务是创建一个机器人,当有人在Discord服务器上发帖时,它可以创建一条tweet。除了Twitter->Discord机器人,我还没有找到任何文档。但这显然是可行的,因为该网站似乎恰恰提供了这一点 到目前为止,我的项目一直在使用discord.js和node.js。当你是开发人员时,不需要雇佣开发人员!我想出来了 `// Discord Modules const Discord = require('discord.js') const fs = require('fs'); cons

我的任务是创建一个机器人,当有人在Discord服务器上发帖时,它可以创建一条tweet。除了Twitter->Discord机器人,我还没有找到任何文档。但这显然是可行的,因为该网站似乎恰恰提供了这一点


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

当你是开发人员时,不需要雇佣开发人员!我想出来了

`// 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)`
以及为了发布tweet而调用的命令

`// 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)
  }
}`

丑陋的小概念证明,但它的工作。下次我一定会问一个更详细的问题

你的问题是什么?Stack Overflow不是一项为您设计、开发和测试代码的服务——如果您有需要转换为工作产品的需求,您应该雇佣一名开发人员。编辑您的问题,将您自己解决问题的尝试包括在内,并根据我们的指南简要说明您在尝试中遇到的具体问题。这个问题太模糊,但如果您知道如何使用API,也很容易解决。第1步:学习如何使用discord api告知何时触发推文。第2步:学习如何使用twitter api制作推文。第三步:???第四步:利润