biscot
biscot

Reputation: 37

Discord Bot Cannot find channel using startWith()

I have been trying for way too long to get this code work, if anyone can help me it would be appreciated.

module.exports = {
    config: {
        name: "close",
        description: "Closes a ticket you have resolved.",
        usage: "-close",
        category: "tickets",
        accessableby: "Members"
    },
    run: async (message) => {
        if (message.channel.name.startsWith('ticket-')) {
            try {
                message.channel.delete();
            } catch (e) {
                console.log(e)
            }

        }
    }

}

This produces an error here:

(node:25380) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined
    at Object.run (/commands/tickets/close.js:10:29)
    at module.exports (/events/guild/message.js:35:33)
    at Client.emit (events.js:189:13)
    at MessageCreateHandler.handle (/Desktop/waterhack bot/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (/Desktop/waterhack bot/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (/Desktop/waterhack bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (/Desktop/waterhack bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17)
    at WebSocket.onMessage (/Desktop/waterhack bot/node_modules/ws/lib/event-target.js:120:16)
    at WebSocket.emit (events.js:189:13)
    at Receiver.receiverOnMessage (/Desktop/waterhack bot/node_modules/ws/lib/websocket.js:789:20)
(node:25380) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 5)

message.channel.name works fine in my other code but now when all I want to do is delete a channel it won't work, any clue of how this could be fixed?

I have tried finding the exact name of the channel by getting the authors name, but yet message.author.username also appears undefined, and that still wouldn't work if a support member would want to close it.

Upvotes: 1

Views: 47

Answers (1)

Tenclea
Tenclea

Reputation: 1464

You might have made an error while passing your message argument. You might have to check wether you made your command call with the correct variable.

Upvotes: 1

Related Questions