我正在尝试使用discord.js和clime用TypeScript编写一个简单的Discord机器人。
我遇到了一个问题,我试图访问传递的上下文对象的对象属性,但是它始终为null。当我使用vscode的调试器或console.log检查属性时,该对象似乎具有我期望的所有属性,只是它们的嵌套层太深了。
export class DiscordCommandContext extends Context {
public message:Message;
public client:Client;
constructor (options:ContextOptions, message:Message, client:Client) {
super(options);
this.message = message;
this.client = client;
}
}
当我尝试访问它的message属性时,它总是虚假的(如果跳过了块)。
if (context.message.guild) {
var settings = await repo.getRealmSettings(+context.message.guild.id);
if (key) {
embed.fields.push({name:key,value:settings[key]});
} else {
Object.keys(settings).forEach(property => {
embed.fields.push({name:property,value:settings[property]});
});
}
}
但是在控制台中,我看到了: DiscordCommandContext appears to have nested "message" objects, one of the wrong type
我无法访问context.message.message,得到的消息是“消息类型上不存在属性'消息'”,这与我期望的一样。
编辑1
我的实例化代码如下:
var options:ContextOptions = {
commands: argArr,
cwd: ""
};
var context = new DiscordCommandContext(options, this.message, this.client );
其中argArr是传递给方法的拆分字符串,并且this.message和this.client都填充在调用类的构造函数中(无为null)
我设法通过将它更改为DiscordCommandContext使其正常运行:
export class DiscordCommandContext extends Context {
public message:Message;
public client:Client;
public realmSettings: RealmSettings;
constructor (options:ContextOptions, contextExtension:DiscordCommandContextValues) {
super(options);
this.message = contextExtension.message;
this.client = contextExtension.client;
this.realmSettings = contextExtension.realmSettings
}
}
export interface DiscordCommandContextValues {
message:Message;
client:Client;
realmSettings: RealmSettings;
}
并这样称呼它:
var context = new DiscordCommandContext(options, {message:this.message, client:this.client, realmSettings: settings} );
我不确定这是否是正确的方法...但是可以。