如何在过滤/收集代码中使用等待运行代码
Posted
技术标签:
【中文标题】如何在过滤/收集代码中使用等待运行代码【英文标题】:How to run code with await in filter/collect code 【发布时间】:2019-09-20 17:41:18 【问题描述】:我正在尝试对我的命令进行确认部分,如果您激活该命令,则需要在代码激活之前说“是”。重复出现的问题是消息后面的代码
已确认...请稍候。
在此之后,它完全跳过代码并且什么都不做。当我在 VSC 中编写代码时,代码的 async
部分没有用黄色突出显示,而是更多的是更深的黄色。
我已尝试删除这部分代码
const async = async () =>
但是除非连接到异步,否则带有等待的代码无法运行。
我试过改变异步的运行方式。
async () =>
结果还是一样。
删除开始的异步代码也只会导致命令中断。
我已经将大块代码放在then(collected
代码之外,但是在命令激活时等待几秒钟后,它立即运行,然后出现值错误。但我希望它在作者说“是”时激活代码
async run(message, args)
if (message.channel instanceof Discord.DMChannel) return message.channel.send('This command cannot be executed here.')
let replyMessage = message.reply('Please make sure that my integration role "Synthibutworse" is above the default role used for this server. Is this correct? [Reply with "YES"] if so. Will expire in 20 seconds...');
let filter = msg => msg.author.id == message.author.id && msg.content.toLowerCase() == 'yes';
message.channel.awaitMessages(filter, max: 1, time: 20000).then(collected =>
message.reply('Confirmed... Please wait.');
const async = async () =>
if(!message.member.guild.me.hasPermission(['MANAGE_WEBHOOKS'])) return message.channel.send('I don\'t have the permissions to make webhooks, please contact an admin or change my permissions!')
if(!message.member.guild.me.hasPermission(['MANAGE_ROLES'])) return message.channel.send('I don\'t have the permissions to make roles, please contact an admin or change my permissions!')
if (!message.member.hasPermission(['MANAGE_WEBHOOKS'])) return message.channel.send('You need to be an admin or webhook manager to use this command.')
if (!message.member.hasPermission(['MANAGE_ROLES'])) return message.channel.send('You need to be an admin or role manager to use this command.')
const avatar = `https://cdn.discordapp.com/attachments/515307677656678420/557050444954992673/Generic5.png`;
const name2 = "SYNTHIBUTWORSE-1.0WOCMD";
let woaID = message.mentions.channels.first();
if(!woaID) return message.channel.send("Channel is nonexistant or command was not formatted properly. Please do s!woa #(channelname)");
let specifiedchannel = message.guild.channels.find(t => t.id == woaID.id);;
const hook = await woaID.createWebhook(name2, avatar).catch(error => console.log(error))
await hook.edit(name2, avatar).catch(error => console.log(error))
message.channel.send("Please do not tamper with the webhook or else the command implied before will no longer function with this channel.")
var role = message.guild.createRole(
name: `Synthibutworse marker v1.0`,
color: 0xcc3b3b,).catch(console.error);
specifiedchannel.send("Created role...");
if(message.guild.roles.name == "Synthibutworse marker v1.0")
role.setMentionable(false, 'SBW Ping Set.')
role.setPosition(1)
role.setPermissions(['CREATE_INSTANT_INVITE', 'SEND_MESSAGES'])
.then(role => console.log(`Edited role`))
.catch(console.error);
message.channel.send("Role set up...");
const sbwrID = message.guild.roles.find(`Synthibutworse marker v1.0`);
let specifiedrole = message.guild.roles.find(r => r.id == sbwrID.id)
message.channel.send('Created Role... Please wait.');
message.guild.specifiedchannel.replacePermissionOverwrites(
overwrites: [
id: specifiedrole,
denied: ['SEND_MESSAGES'],
allowed: ['VIEW_CHANNEL'],
,
],
reason: 'Needed to change permissions'
);
var bot = message.client
bot.on('message', function(message)
if(webhook.name == `Synthibutworse marker`) return
if(message.channel.id == webhook.channelID) return
let bannedRole = message.guild.roles.find(role => role.id === specifiedrole);
message.member.addRole(bannedRole);
);
);
;
module.exports = woa;
我希望命令以异步方式运行,并在消息作者说“是”时继续执行代码,而不是在 Please wait.
消息处停止。
实际发生的情况是代码没有在Please wait
消息之后立即运行。
【问题讨论】:
【参考方案1】:在您当前的代码中,您声明了一个异步函数,但从未调用它。这就是代码从不运行以及 VSC 将其名称变暗的原因。
解决方案:
将您的awaitMessages(...)
的.then()
回调定义为async
:
.then(async collected =>
// do async stuff
);
为了将来参考,如果您需要在无法将函数定义为异步的地方使用异步代码,请使用以下设置:
(async function()
// do async stuff
)();
【讨论】:
以上是关于如何在过滤/收集代码中使用等待运行代码的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Vscode 中的 Typescript 中等待后调试代码?