Reputation:
I am making a bot that can announce messages when specific events happen. Does anyone know how I can output a message to a specific text channel?
Upvotes: 3
Views: 27987
Reputation: 180
What I did was: used dependency injection to have the logged-in client available wherever I wanted to send the message, in my case, it was a Module.
public XModule(DiscordSocketClient client){
this.client = client;
}
Then as @slothgod said:
private async Task Announce(ExampleType object) //1
{
ulong id = object.ChannelId; // 3
var chnl = client.GetChannel(id) as IMessageChannel; // 4
await chnl.SendMessageAsync("Message!"); // 5
}
Upvotes: 0
Reputation: 63
As @slothgod said
public async Task Announce() // 1
{
DiscordSocketClient _client = new DiscordSocketClient(); // 2
ulong id = 123456789012345678; // 3
var chnl = _client.GetChannel(id) as IMessageChannel; // 4
await chnl.SendMessageAsync("Announcement!"); // 5
}
The above code works when you want to send reply pre-fixed messages(Like "Announcement!" in above code) in only one channel. You can change the channel in "ulong id = ....your channel id here....;"
But if you want to send custom messages to custom channel then try this code.
public async Task _say(IMessageChannel chh,[Remainder]string repeat)
{
await Context.Message.DeleteAsync();
await chh.SendMessageAsync(repeat); // 5
}
To use the code I sent just type this command : "Your Bot Prefix and Command" "Channel name or ID" "Message you want to send"
Example : DFH.say TestChannel Welcome to this channel
Upvotes: 0
Reputation: 1
public async Task TalkInChannel() //1
{
DiscordSocketClient _client = new DiscordSocketClient(); //2
var channel = _client.GetChannel(Channel ID) as SocketGuildChannel; //3
await channel.SendMessageAsync("Message Here!"); //4
}
You can also use embeds by creating an Embed then replacing "Message Here!" with """, false EmbedName.Build()"
Upvotes: -1
Reputation: 376
public async Task Announce() // 1
{
DiscordSocketClient _client = new DiscordSocketClient(); // 2
ulong id = 123456789012345678; // 3
var chnl = _client.GetChannel(id) as IMessageChannel; // 4
await chnl.SendMessageAsync("Announcement!"); // 5
}
[1] A standard async task (You can choose to just include the code inside the braces inside your own method).
[2] Create an instance of the Discord Client.
[3] A random sequential channel ID (Replace with your own channel ID).
[4] Creating an instance of the Discord Channel as an IMessageChannel instead of a SocketChannel.
[5] Sending the message to the text channel.
Upvotes: 5