C
C#15mo ago
Taha

❔ My discord bot Does work in DMs but servers

Today i wrote a simple C# bot in .NET framework. It Works well in dms but it does not answer my commands in a server(btw im using prefix commands not slash ones)im pretty sure i turned on intents Also while my bot is starting it says "You're using the GuildScheduledEvents gateway intent without listening to any events related to that Gateway. Consider removing the intent from your config" I dont know what that means.
83 Replies
Kotz
Kotz15mo ago
Out of curiosity - is there a reason you're using .NET Framework instead of .NET Core?
mtreit
mtreit15mo ago
There is a #discord-dev channel that might be of help.
Taha
TahaOP15mo ago
Um no? Kk tysm!
CoRoys
CoRoys15mo ago
You need to specifically tell the DiscordClient to use the Message intents
Taha
TahaOP15mo ago
how?
CoRoys
CoRoys15mo ago
one min let me fire up my project
CoRoys
CoRoys15mo ago
CoRoys
CoRoys15mo ago
Add the intents from here
CoRoys
CoRoys15mo ago
Then start the client from that config
Taha
TahaOP15mo ago
Ohh tysm! So i make a new method I mean
CoRoys
CoRoys15mo ago
Share a snippet of your code
Taha
TahaOP15mo ago
Where of it?
CoRoys
CoRoys15mo ago
Where you start the client
Taha
TahaOP15mo ago
I dont have a thing like that in my code
CoRoys
CoRoys15mo ago
Program.cs?
Taha
TahaOP15mo ago
Also
CoRoys
CoRoys15mo ago
What Discord library are you using?
Taha
TahaOP15mo ago
my _client = new DiscordSocketClieny() the parantes is empty Discord.NET In what method should i add that?
CoRoys
CoRoys15mo ago
Replace it with this
Taha
TahaOP15mo ago
Ok tysm!!
CoRoys
CoRoys15mo ago
Wait not that It's not editing lol
_client = new DiscordSocketClient(new DiscordSocketConfig
{
AlwaysDownloadUsers = true,
MessageCacheSize = 1000,
GatewayIntents = DirectMessages
| DirectMessageTyping
| MessageContent
});
_client = new DiscordSocketClient(new DiscordSocketConfig
{
AlwaysDownloadUsers = true,
MessageCacheSize = 1000,
GatewayIntents = DirectMessages
| DirectMessageTyping
| MessageContent
});
Replace it with this
Taha
TahaOP15mo ago
Ok i try that! Tysm for help!
CoRoys
CoRoys15mo ago
np you're on .NET Framework so you need to include parentheses for initialisation:
_client = new DiscordSocketClient(new DiscordSocketConfig()
{
AlwaysDownloadUsers = true,
MessageCacheSize = 1000,
GatewayIntents = DirectMessages
| DirectMessageTyping
| MessageContent
});
_client = new DiscordSocketClient(new DiscordSocketConfig()
{
AlwaysDownloadUsers = true,
MessageCacheSize = 1000,
GatewayIntents = DirectMessages
| DirectMessageTyping
| MessageContent
});
highly suggest you migrate to new .NET versions and upgrade your language version
Taha
TahaOP15mo ago
Ok! Ty And a problem It does not recognize MessageCacheSize i have no idea why Am i missing a using or smth?
CoRoys
CoRoys15mo ago
no just remove it Probably has a different name You're running an older version of the library
Taha
TahaOP15mo ago
So its not necessary?
CoRoys
CoRoys15mo ago
depends on your bot's functionalities
Taha
TahaOP15mo ago
I found a class called MessageCache
CoRoys
CoRoys15mo ago
It's probably that
Taha
TahaOP15mo ago
But not a field
Kotz
Kotz15mo ago
You should seriously reconsider your choice then, because .NET Framework is on maintenance mode and thus not receiving new features or being worked on anymore.
Taha
TahaOP15mo ago
Is it supposed to be a class? I know
Kotz
Kotz15mo ago
If you know, then why did you...
Taha
TahaOP15mo ago
I use .NET 8 most of the time For everything
CoRoys
CoRoys15mo ago
It's not really necessary but either way you should upgrade Discord.Net to 3.9 because the Discord API changes very often Also please upgrade to .NET 7
Kotz
Kotz15mo ago
.NET 8 is not even released yet
Taha
TahaOP15mo ago
i mean .NET 7 Sorry Im on 2022 Vs I have to write all my codes again ? I have .net 7 Already
Kotz
Kotz15mo ago
Then you're not using .NET Framework
CoRoys
CoRoys15mo ago
Can't judge but definitely not all of it Some platform specific stuff will not be supported I think
Taha
TahaOP15mo ago
My cousin told me to make sure its .net framework
Kotz
Kotz15mo ago
Why?
Taha
TahaOP15mo ago
Bc he was not used to new thing Discord.net Things
Kotz
Kotz15mo ago
Well, your cousin is a dumbass.
Taha
TahaOP15mo ago
oh:((
CoRoys
CoRoys15mo ago
Example of migrating to the latest .NET - .NET
Learn how to migrate your applications from .NET 5, .NET Core, and .NET Framework to .NET 7.
Taha
TahaOP15mo ago
Am i fucked up now?
CoRoys
CoRoys15mo ago
No doesn't take more than 30 minutes to migrate in my experience
Taha
TahaOP15mo ago
I swear i have never used .net framework in my life but this time
Kotz
Kotz15mo ago
There is literally no reason to use .NET Framework for new projects. Some libraries don't even support it anymore. I don't know if that's the case for Discord.Net, but I do know it is for DSharpPlus and Remora.
CoRoys
CoRoys15mo ago
WinForms, WPF, and UWP cough cough
Kotz
Kotz15mo ago
Those all work on .NET Core.
Taha
TahaOP15mo ago
So lemme try without that thing The cachesize Btw @OfType<T>()
CoRoys
CoRoys15mo ago
They're not cross-platform so why would you deploy a self-contained 150mb copy to an end user when you can just do 10mb with .NET Framework?
Taha
TahaOP15mo ago
Is the massagecachesize a field ??
Kotz
Kotz15mo ago
That's a different discussion.
CoRoys
CoRoys15mo ago
It is yeah that's my point. I think newer .NET versions should always be shipped with Windows updates
Taha
TahaOP15mo ago
Ok
Kotz
Kotz15mo ago
So Windows users get 10mb binaries, but other platforms keep getting 60mb ones?
Taha
TahaOP15mo ago
Ok lets see if it works
CoRoys
CoRoys15mo ago
last I checked WinForms and WPF are not even cross-platform so
Kotz
Kotz15mo ago
Oh right Fair
Taha
TahaOP15mo ago
Its connecting NO WAY The error is fixed The error that i said upper But it did not work Wait maybe i should restart it
CoRoys
CoRoys15mo ago
that was a warning not an error make sure you're not filtering the MessageReceived event
Taha
TahaOP15mo ago
Okay
CoRoys
CoRoys15mo ago
_client.MessageReceived += (m) => _ = Task.Run(() => HandleMessageAsync(m));
_client.MessageReceived += (m) => _ = Task.Run(() => HandleMessageAsync(m));
Subscribe to the event like this
private async Task HandleMessageAsync(SocketMessage socketMessage)
{
var argPos = 0;
if (socketMessage.Author.IsBot)
return;

if(socketMessage is not SocketUserMessage message)
return;
...
private async Task HandleMessageAsync(SocketMessage socketMessage)
{
var argPos = 0;
if (socketMessage.Author.IsBot)
return;

if(socketMessage is not SocketUserMessage message)
return;
...
Place breakpoints on your socketMessage. See if its Content property contains any value.
Taha
TahaOP15mo ago
So where should i put this code I mean what method Oh ok
CoRoys
CoRoys15mo ago
Where you subscribe to the events
Taha
TahaOP15mo ago
Look at this @OfType<T>()
private async Task HandleCom(SocketMassege args)
{
var msg = args as SocketUserMassege;
var context = new SocketCommandContext(_client,msg)
if(msg.Author.IsBot)
return;

Int argPos = 0;
If (msg.HasStringPrefix("$" ,ref argPos)
{
Var res = await _command.ExcuteAsync(context,argPos,_services)
If(!res.Issuccses) console.writle(msg.errorreason)
}



}
private async Task HandleCom(SocketMassege args)
{
var msg = args as SocketUserMassege;
var context = new SocketCommandContext(_client,msg)
if(msg.Author.IsBot)
return;

Int argPos = 0;
If (msg.HasStringPrefix("$" ,ref argPos)
{
Var res = await _command.ExcuteAsync(context,argPos,_services)
If(!res.Issuccses) console.writle(msg.errorreason)
}



}
mtreit
mtreit15mo ago
If you're going to share code snippets you should make sure they are not full of typos Did you type that in a word processor?
CoRoys
CoRoys15mo ago
Place a breakpoint after msg. See if its Content property is null
Taha
TahaOP15mo ago
I didnt copy that code Im on phone in my discord
CoRoys
CoRoys15mo ago
$10 typed on a phone crap
mtreit
mtreit15mo ago
💵
CoRoys
CoRoys15mo ago
I was one second late
Taha
TahaOP15mo ago
Ok wait Still does not work
CoRoys
CoRoys15mo ago
obviously that's the whole point of debugging you need to check this
Taha
TahaOP15mo ago
I tried that
CoRoys
CoRoys15mo ago
and? Is Content null?
Taha
TahaOP15mo ago
Wait I searched in google and amd it said smth is wrong with my intent Is that the code which activates the intents?
CoRoys
CoRoys15mo ago
Yeah
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server