简体   繁体   中英

Can I have help for my error ts(7053) on typescript?

Hello so here I wanted to code a discord bot with discord.js (node.js) and extentions like opusscript and ytdl-core and I have as error if with this code. Thanks for your answer.

import * as Discord from "discord.js";
import {IBotCommand} from "../api";

var servers = {};

const ytdl = require("ytdl-core");

export default class play implements IBotCommand {

   private readonly _command = "play"

    help(): string {
        return "Cette commande ne fait absolument rien c'est juste pour le fun :)";
    }

    isThisCommand(command: string): boolean {
        return command === this._command;
    }

    async runCommand(args: string[], msgObject: Discord.Message, client:Discord.Client): Promise<void> {     

        function play(connection: Discord.VoiceConnection, msgObject: Discord.Message){
            var server = servers[msgObject.guild.id];


            server.dispatcher = connection.playStream(ytdl(server.queue[0], {filter: "audionly"}));

            server.queue.shift();

            server.dispatcher.on("end", function(){
                if(server.queue[0]){
                    play(connection, msgObject);
                } else {
                    connection.disconnect();
                }
            })
        }    

        if(!args[1]) {
            msgObject.channel.send("Vous devez donner un lien valide !");
            return;
        }

        if(!msgObject.member.voiceChannel) {
            msgObject.channel.send("Vous devez être dans un channel vocal pour jouer une musique !");
            return;
        }

        if(!servers[msgObject.guild.id]) servers[msgObject.guild.id] = {
            queue: []
        }

        var server = servers[msgObject.guild.id];

        server.queue.push(args[1]);

        if(!msgObject.guild.voiceConnection) msgObject.member.voiceChannel.join().then(function(connection){
            play(connection, msgObject);
        }) 
    }
}

error ts(7053)

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'. No index signature with a parameter of type 'string' was found on type '{}'.

You'll need to add a type to servers . At the moment, typescript is taking its best guess, and it assumes it's an empty object. You can use an index type , something like this (i don't exactly know what you want the Server type to look like, so feel free to make it more specific to meet your needs):

interface Server {
  queue: any[]
}

var servers: { [key: string]: Server } = {};

I assume problem is with line:

var server = servers[msgObject.guild.id];

TS is inferencing server as empty object type, and is right as you put there empty object. It means that you cannot use any property of this object as there are no properties. To fix this you need to at least define servers as some key->value map like

var servers: Record<string, any> = {} // any mean that value can be anything

But after this will work but you will have no type check in next lines, as you use server as a thing which has methods like dispatch . So to have it working properly on type level you need to define servers type in more specific way, so as I started from Record<string, any> you need to be more precise and define the Server type also and put it as Record<string, Server>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM