[英]discord.js v12 send message to first channel
嗨,我如何將消息發送到機器人可以在 Discord.js v12 中發送消息的第一個頻道。 請幫我。 這對我不起作用:
client.on("guildCreate", guild => {
let channelID;
let channels = guild.channels;
channelLoop:
for (let c of channels) {
let channelType = c[1].type;
if (channelType === "text") {
channelID = c[0];
break channelLoop;
}
}
let channel = client.channels.get(guild.systemChannelID || channelID);
channel.send(`Thanks for inviting me into this server!`);
});
你可以這樣做。
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("guildCreate", (guild) => {
const channel = guild.channels.cache.find(
(c) => c.type === "text" && c.permissionsFor(guild.me).has("SEND_MESSAGES")
);
if (channel) {
channel.send(`Thanks for inviting me into this server!`);
} else {
console.log(`can\`t send welcome message in guild ${guild.name}`);
}
});
針對 Discord v13 更新
請注意c.type
已從text
更改為GUILD_TEXT
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("guildCreate", (guild) => {
const channel = guild.channels.cache.find(
(c) => c.type === "GUILD_TEXT" && c.permissionsFor(guild.me).has("SEND_MESSAGES")
);
// Do something with the channel
});
從 v13 更新到 v14
guildCreate
的事件現在是Events.GuildCreate
c.type
從GUILD_TEXT
更改為ChannelType.GuildText
guild.me
現在是guild.members.me
const { Events, ChannelType } = require('discord.js')
const client = new Client({
//your intents here
intents: []
})
client.on(Events.GuildCreate, guild => {
const channel = guild.channels.cache.find(c =>
c.type === ChannelType.GuildText &&
c.permissionsFor(guild.members.me).has('SEND_MESSAGES')
)
//do stuff with the channel
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.