【问题标题】:Having some trouble with weather-js and argsWeather-js 和 args 有一些问题
【发布时间】:2019-09-12 00:48:24
【问题描述】:

我的机器人中有一个命令,叫做天气,它工作正常,但是如果用户在没有任何参数的情况下编写它,我想发送一条错误消息。

如果参数不是一个地方,它可以工作,但如果你写它没有任何参数,它不会回复任何东西

这是代码(更新了整个代码)

      const Discord = require('discord.js');
const weather = require('weather-js');

exports.run = async (client, message, args) => {

    weather.find({search: args[0], degreeType: "C"}, function(err, result){
        if (err) message.channel.send(err);

        const noargs = new Discord.RichEmbed()
        .setDescription(`Input a valid location, please`)
        .setColor(0xfd5454)

        if(!result.length) {
            message.channel.send(noargs);
            return;
        }

        var current = result[0].current;
        var location = result[0].location;

如果你写“,weather nonexistingcity”,它可以工作,但如果你写“,weather”没有任何参数,它就不起作用。 PD:noargs 是一个不和谐的嵌入,已声明但未包含在这篇文章中。

【问题讨论】:

  • 你如何声明你的论点?
  • 显示的代码太少,无法完整....minimal reproducible example
  • 要我发布完整代码吗?
  • 不,只有相关代码。
  • 好的,我发布了相关的

标签: javascript bots discord discord.js


【解决方案1】:

如果你想检查是否没有 args 你可以这样做:

if(args.length == 0) return message.channel.send('You need to provide a city');

【讨论】:

  • 是的,但是如果用户没有为 args 提供默认消息,weather-js 被编程为回答用户。我想知道如何更改该消息。这是一个屏幕截图。蓝色是我编程的。红色是作者编程回答的,我不知道怎么改dropbox.com/s/st9n4lki6ed7vih/…
  • 是的,只需在天气 js 之前执行if(args.length == 0) return message.channel.send(noargs)。 Obv 还移动了const noargs =,它将检查是否有人在没有任何输入的情况下做了;weather 并发送嵌入
【解决方案2】:

...weather-js 被编程为在用户未向 args 提供默认消息时回答用户。我想知道如何更改该消息。

如果您真的想要更改此消息,而不是像@PLASMA chicken 建议的那样自己检查您的论点,该消息位于weather-js/index.js 的第 31 行。

【讨论】:

    【解决方案3】:
    const weather = require('weather-js');
    
    const Discord = require('discord.js');
    
    module.exports = {
        name: "weather",
        description: "Checks a weather forecast",
    
        async execute(client, message, cmd, args, Discord){
    
        weather.find({search: args.join(" "), degreeType: 'F'}, function (error, result){
            // 'C' can be changed to 'F' for farneheit results
            if(error) return message.channel.send(error);
            if(!args[0]) return message.channel.send('Please specify a location')
    
            if(result === undefined || result.length === 0) return message.channel.send('**Invalid** location');
    
            var current = result[0].current;
            var location = result[0].location;
    
            const weatherinfo = new Discord.MessageEmbed()
            .setDescription(`**${current.skytext}**`)
            .setAuthor(`Weather forecast for ${current.observationpoint}`)
            .setThumbnail(current.imageUrl)
            .setColor(0x111111)
            .addField('Timezone', `UTC${location.timezone}`, true)
            .addField('Degree Type', 'Celsius', true)
            .addField('Temperature', `${current.temperature}°`, true)
            .addField('Wind', current.winddisplay, true)
            .addField('Feels like', `${current.feelslike}°`, true)
            .addField('Humidity', `${current.humidity}%`, true)
    
    
            message.channel.send(weatherinfo)
            })        
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多