如何在过滤器/收集代码中以等待方式运行代码

时间:2019-05-02 08:26:44

标签: discord.js commando

我正在尝试对命令进行确认,如果您激活命令,则需要在激活代码之前说“是”。经常出现的问题是,消息提示后的代码

  

已确认...请稍候。

此后,它将完全跳过代码,并且不执行任何操作。当我用VSC编写代码时,代码的async部分没有用黄色突出显示,而是用更深的黄色突出显示。

我尝试删除了这部分代码 const async = async () => { 但是带有await的代码除非连接到异步,否则无法运行。 我尝试更改异步的运行方式。 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 个答案:

答案 0 :(得分:0)

在当前代码中,您正在声明一个异步函数,但从未调用它。这就是为什么代码永远不会运行以及VSC使其名称变暗的原因。

解决方案:
awaitMessages(...)的{​​{1}}回调定义为.then()

async

供以后参考,如果需要在无法将函数定义为异步的地方使用异步代码,请使用以下设置:

.then(async collected => {
  // do async stuff
});