[英]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.您好,所以在这里我想用 discord.js (node.js) 和 opusscript 和 ytdl-core 等扩展来编写 discord 机器人,如果使用此代码,我会遇到错误。 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)
错误 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
.您需要向
servers
添加一个类型。 At the moment, typescript is taking its best guess, and it assumes it's an empty object.目前,typescript 正在做出最好的猜测,它假设它是一个空的 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):
您可以使用index type ,像这样(我不完全知道您希望 Server 类型看起来像什么,所以请随意使其更具体以满足您的需求):
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. TS 将
server
推断为空 object 类型,并且在您将空 object 放在那里时是正确的。 It means that you cannot use any property of this object as there are no properties.这意味着您不能使用此 object 的任何属性,因为没有属性。 To fix this you need to at least define servers as some key->value map like
要解决此问题,您至少需要将服务器定义为一些键->值 map
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
.但是在这之后会起作用,但是您将不会在下一行中进行类型检查,因为您将 server 用作具有
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>
因此,要让它在类型级别上正常工作,您需要以更具体的方式定义服务器类型,所以当我从
Record<string, any>
开始时,您需要更精确并定义服务器类型并将其作为Record<string, Server>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.