• Hey zusammen!

    Ich programmiere gerade wieder einen kleinen Discord Bot und habe 'nen Fehler mit await und async.

    Ich versuche mit Reactions zu arbeiten, das sieht wie folgt aus:

    client.on("messageReactionAdd", async (reaction, user) => {
    
        if(reaction.emoji.name !== "📌") return;
        client.commands.get("pin").execute(reaction, user);
        
    });
    

    Das Gegenstück schaut so aus:

    module.exports = {
        name: "pin",
        description: "This pins a message",
        execute(reaction, user){
    
            await reaction.message.reply("You have pinned this message! :pushpin:");
    
        }
    }
    

    Beim Ausführen bekomme ich jedoch folgenden Fehler:

    /home/users/cooper/discord-bots/discord-bot/commands/pin.js:10
            await reaction.message.reply("You have pinned this message! :pushpin:");
    
    SyntaxError: await is only valid in async function
    

    Ich verstehe das nicht ganz? Die Funktion wird doch innerhalb einer async-Funktion aufgerufen, oder nicht… ? o.O

    Wäre nice wenn ihr mir helfen könntet … 😄


  • awaits kannst du nur in funktionen verwenden, die asynchron sind also z.b.

    const main = async (reaction) => {
      await reaction.message.reply("You have pinned this message! :pushpin:");
    }
    
    
    module.exports = {
        name: "pin",
        description: "This pins a message",
        execute(reaction, user){
          main(reaction);      
        }
    }
    

  • async execute(


  • @markxoe schrieb:

    awaits kannst du nur in funktionen verwenden, die asynchron sind also z.b.

    const main = async (reaction) => {
      await reaction.message.reply("You have pinned this message! :pushpin:");
    }
    
    
    module.exports = {
        name: "pin",
        description: "This pins a message",
        execute(reaction, user){
          main(reaction);      
        }
    }
    

    Okay also ich dachte ich habe hiermit dann eine async-Funktion:

    client.on("messageReactionAdd", async (reaction, user) => {
        if(reaction.emoji.name !== "📌") return;
        client.commands.get("pin").execute(reaction, user); 
    });
    

    Naja okay, also mache ich eine zweite async-Funktion die ich dann wiederum über das Modul aufrufe 🤣 muss ich nicht verstehen aber funktioniert. Danke 👍

    So siehts jetzt aus:

    const Discord = require("discord.js");
    const fs = require("fs");
    const config = JSON.parse(fs.readFileSync("./utils/config.json", "utf8"));
    
    const main = async (reaction, user) => {
        await reaction.message.reply("You have pinned this message! :pushpin:");
    }
    
    module.exports = {
        name: "pin",
        description: "This pins a message",
        execute(reaction, user){
            main(reaction, user);
        }
    }
    

  • @Nlmyr schrieb:

    async execute(

    Okay lol danke. 😄

    Funktioniert :facepalm: Danke euch

    Variante 2:

    const Discord = require("discord.js");
    const fs = require("fs");
    const config = JSON.parse(fs.readFileSync("./utils/config.json", "utf8"));
    
    module.exports = {
        name: "pin",
        description: "This pins a message",
        async execute(reaction, user){
            await reaction.message.reply("You have pinned this message! :pushpin:");
        }
    }
    

  • Haha lol


  • 😅😂

Ähnliche Themen

  • 2
  • 1
  • 4
  • 3
  • 1