I'm trying to create a discord bot in .NET with a command to put a user on markers. For example, a server admin gives a user 50 markers and the user would get all of his roles removed and would be given a role called "Markers". Then, a text channel will appear where the user needs to spam 50 messages to remove the markers and he'd get everything back. I have made commands for giving markers and removing them but when I try to remove someone's markers by calling the function from the Program.cs it gives a null pointer exception for some reason. Here's the code:
Program.cs event:
private async Task MessageRecieved(SocketMessage arg)
{
//Checking if the user has markers
var roles = (arg.Author as SocketGuildUser).Roles;
bool imaMarkere = false;
foreach (var role in roles)
{
if (role.Id == 844558286611152917)
imaMarkere = true;
}
if (arg.Channel.Id == 844558185024585770 && imaMarkere)
{
int markerCount = 0;
using (SqliteConnection con = new SqliteConnection(connectionString))
{
con.Open();
string query = $"select markercount from markers";
var com = con.CreateCommand();
com.CommandText = query;
var rdr = com.ExecuteReader();
if (rdr.Read())
{
markerCount += Convert.ToInt32(rdr["markercount"]) - 1;
}
rdr.Close();
//Removing 1 marker per message recieved
if (markerCount > 0)
{
com.CommandText = $"update markers set markercount={markerCount} where userid='{arg.Author.Id}'";
com.ExecuteNonQuery();
}
//If no markers are left returning the user roles
else
{
Commands c = new Commands();
await c.Skini(arg.Author as IGuildUser);
}
con.Close();
}
}
else return;
}
Commands.cs
[Command("skini"), RequireUserPermission(GuildPermission.Administrator, ErrorMessage = "Nemate potrebnu dozvolu ``ADMINISTRATOR``")]
public async Task Skini(IGuildUser user = null)
{
if(user == null)
{
await ReplyAsync($"{Context.User.Mention},kome da skinem?");
}
string rolesString = "";
string[] roles;
using (SqliteConnection con = new SqliteConnection(connectionString))
{
con.Open();
var com = con.CreateCommand();
string readQuery = "select pastroles from markers";
com.CommandText = readQuery;
var rdr = com.ExecuteReader();
if (rdr.Read())
{
rolesString += rdr["pastroles"].ToString();
}
rdr.Close();
string deleteQuery = $"delete from markers where userid = '{user.Id}'";
com.CommandText = deleteQuery;
com.ExecuteNonQuery();
con.Close();
}
roles = rolesString.Split(",");
foreach (var roleid in roles)
{
var role = Context.Guild.GetRole(Convert.ToUInt64(roleid));//this line is where the exception happens
if(!role.IsEveryone)
await user.AddRoleAsync(role);
}
var roleMarkeri = Context.Guild.GetRole(844558286611152917);
await user.RemoveRoleAsync(roleMarkeri);
var EmbedBuilder = new EmbedBuilder()
.WithTitle("**MARKERI**")
.WithColor(Color.Blue)
.WithDescription($":white_check_mark: {Context.User.Mention} je **skinuo** markere {user.Username}")
.WithFooter(footer =>
{
footer
.WithText("Marker log");
});
Embed embed = EmbedBuilder.Build();
await ReplyAsync(embed: embed);
}
Related
I need to create a login that displays user data after login in a gridview / datatable
I'm able to display the username / password but not the user ID that's needed.
I've also tried creating a class where the values gets stored after login
private bool DataValidation(string user, string pass)
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
using (MySqlCommand cmd = new MySqlCommand("SELECT * "+
"FROM member " +
"WHERE username=#user AND password=#pass;", conn))
{
cmd.Parameters.AddWithValue("#user", user);
cmd.Parameters.AddWithValue("#pass", pass);
cmd.Connection = conn;
cmd.Connection.Open();
MySqlDataReader login = cmd.ExecuteReader();
List<Connect> connectList = new List<Connect>();
while (login.Read())
{
Connect connect = new Connect();
connect.username = login.GetString(0);
connect.password = login.GetString(1);
connect.userID = login.GetString(4);
connectList.Add(connect);
}
if(connectList.Count > 0)
{
return true;
}
else
{
return false;
}
}
I'm mostly not sure how to store or display the values after they have been queried
Based on our conversation in the comments, let's focus on this code:
MySqlDataReader login = cmd.ExecuteReader();
if (login.Read())
{
conn.Close();
return true;
}
else
{
conn.Close();
return false;
}
The only thing we are doing here is finding out if login contains any rows. But we are not doing anything with those rows.
Let's try something like this instead:
MySqlDataReader login = cmd.ExecuteReader();
while (login.Read())
{
var something1 = login.GetString(0); // this will get the value of the first column
var something2 = login.GetString(1); // this will get the value of the second column
}
Or, if you are adding the results to an object:
MySqlDataReader login = cmd.ExecuteReader();
List<MyObject> myObjectList = new List<MyObject>;
while (login.Read())
{
MyObject myObject = new MyObject;
myObject.something1 = login.GetString(0); // this will get the value of the first column
myObject.something2 = login.GetString(1); // this will get the value of the first column
myObjectList.Add(myObject);
}
if (myObjectList.Count > 0)
{
return true;
}
else
{
return false;
}
You will need to adjust to meet your needs, but hopefully, this will get you there.
More info here: SqlDataReader.Read Method
I've a simple ASP.net webform application with identity+owin for authentication. This works perfectly fine.
Now I need to create a webservice(.asmx) to get my users authenticated through service. So the service can accept the UserName+Password and authenticate them.
This is required to authenticate the users for android app.
I don't want to use any other authentication like google or facebook..etc...
And no MVC either.
Can anyone pls point me towards solution.
bellow is my login.aspx page's code behind. What should I put in my webservice.?
Android app developer is another guy. I just need to make sure my webservice works fine.
protected void SignIn(object sender, EventArgs e)
{
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var user = userManager.Find(UserName.Text, Password.Text);
if (user != null)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
string cs = ConfigurationManager.ConnectionStrings["DB_6"].ConnectionString;
using (SqlConnection conn = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetUser", conn);
SqlDataAdapter Adpt = new SqlDataAdapter();
DataSet login = new DataSet();
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#PEmail", UserName.Text);
Adpt.SelectCommand = cmd;
Adpt.Fill(login);
foreach (DataRow dr in login.Tables[0].Rows)
{
string PFName = login.Tables[0].Rows[0]["PFName"].ToString();
string PLName = login.Tables[0].Rows[0]["PLName"].ToString();
string PType = login.Tables[0].Rows[0]["PType"].ToString();
int PersonID = int.Parse(login.Tables[0].Rows[0]["PersonID"].ToString());
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd1 = new SqlCommand("spInsLoc", con);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.Add("#PersonID", SqlDbType.Int).Value = PersonID;
cmd1.Parameters.Add("#OrgID", SqlDbType.Int).Value = 0;
cmd1.Parameters.Add("#Location", SqlDbType.NVarChar).Value = hdnLocation.Value;
cmd1.Parameters.Add("#strOwner", SqlDbType.VarChar).Value = UserName.Text;
cmd1.Parameters.Add("#dbTstamp", SqlDbType.DateTime2).Value = DateTime.Now;
con.Open();
cmd1.ExecuteNonQuery();
}
}
}
catch { }
finally { }
}
Response.Redirect("~/Login.aspx");
}
else
{
StatusText.Text = "Invalid username or password.";
LoginStatus.Visible = true;
}
}
protected void SignOut(object sender, EventArgs e)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
authenticationManager.SignOut();
Response.Redirect("~/Login.aspx");
}
You need use Post&Get method in web service.
Create new android app and use volley or native java library for Http Req.
this is sample code for a request;
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://yoursite.com?name=foo?pass=foo");
urlConnection = (HttpURLConnection) url
.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
System.out.print(current);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
and your web service will response the request.
ex: yoursite.com?name=foo?pass=foo RESPONSE->TRUE or FALSE
I want to retrieve multiple rows from SQL Server in a web service. I get 2 rows as result when I search jo number 1 and my table name is TestOrderStatus.
While I get only one row in the search result.
Thanks for all
public class ReturnOrder
{
public string Message;
public int QtqSlit;
public int QtyPcs;
public string Design;
}
[WebMethod(MessageName = "OrderStatus", Description = "OrderStatus new Order")]
[System.Xml.Serialization.XmlInclude(typeof(ReturnOrder))]
public ReturnOrder OrderStatus(string JO) /// get list of notes
{
int QtqSlit = 0;
int QtyPcs = 0;
String Design = "";
string Message = "";
SqlDataReader reader;
using (SqlConnection connection = new SqlConnection(DBConnection.ConnectionString))
{
SqlCommand cmd = new SqlCommand("SELECT QtqSlit,QtyPcs,Design FROM TestOrderStatus where JO=#JO");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("#JO", JO);
connection.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
QtqSlit = reader.GetInt32(0);
QtyPcs = reader.GetInt32(1);
Design = reader.GetString(2);
}
}
if (QtqSlit == 0)
{
Message = " user name or password is incorrect";
}
reader.Close();
connection.Close();
}
ReturnOrder rt = new ReturnOrder();
rt.Message = Message;
rt.QtqSlit = QtqSlit;
rt.QtyPcs = QtyPcs;
rt.Design = Design;
return rt;
}
You need a List for multiple rows if you want more than one result. So your code may be like this.
public class ReturnOrder
{
public string Message;
public int QtqSlit;
public int QtyPcs;
public string Design;
}
[WebMethod(MessageName = "OrderStatus", Description = "OrderStatus new Order")]
[System.Xml.Serialization.XmlInclude(typeof(List<ReturnOrder>))]
public List<ReturnOrder> OrderStatus(string JO) /// get list of notes
{
List<ReturnOrder> result=new List<ReturnOrder>();
int QtqSlit = 0;
int QtyPcs = 0;
String Design = "";
string Message = "";
//try
//{
SqlDataReader reader;
using (SqlConnection connection = new SqlConnection(DBConnection.ConnectionString))
{
SqlCommand cmd = new SqlCommand("SELECT QtqSlit,QtyPcs,Design FROM TestOrderStatus where JO=#JO");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("#JO", JO);
connection.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
QtqSlit = reader.GetInt32(0);
QtyPcs = reader.GetInt32(1);
Design = reader.GetString(2);
ReturnOrder rt = new ReturnOrder();
rt.Message = Message;
rt.QtqSlit = QtqSlit;
rt.QtyPcs = QtyPcs;
rt.Design = Design;
result.add(rt);
}
if (QtqSlit == 0)
{
Message = " user name or password is in correct";
}
reader.Close();
connection.Close();
}
//}
//catch (Exception ex)
//{
// Message = " cannot access to the data";
//}
return result;
}
Thank you to much now web service is ok , But when he summoned the values in Xamrian Android doesn't know.
public class Mainlistview : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Mainlistview);
ListView ListView = FindViewById<ListView>(Resource.Id.listView1);
Selling.WebServiceDB ws = new Selling.WebServiceDB();
ws.OrderStatusListCompleted += Ws_OrderStatusListCompleted;
ws.OrderStatusListAsync(Convert.ToString(1));
}
private void Ws_OrderStatusListCompleted(object sender, Selling.OrderStatusListCompletedEventArgs e)
{
ListView ListView = FindViewById<ListView>(Resource.Id.listView1);
string msg = "";
if (e.Result.QtqSlit.ToString().Equals("0"))
{
msg = e.Result.Message;
}
else
{
// full class
List<TableItem> tableItems = new List<TableItem>();
tableItems.Add(new TableItem("" + e.Result.QtqSlit, "" + e.Result.QtyPcs, Resource.Drawable.Icon));
ListView.Adapter = new HomeScreenAdapter(this, tableItems);
}
}
Hello and thanks for reading this.
This is my NotificationHub.cs
public class NotificationHub : Hub
{
private static readonly ConcurrentDictionary<string, User> Users = new ConcurrentDictionary<string, User>(StringComparer.InvariantCultureIgnoreCase);
private static List<User> UserList = new List<User>();
Int16 totalNewMessages = 0;
string UserID;
[HubMethodName("check")]
public Task Check(string id)
{
string profileId = id; //Context.QueryString["id"];
string connectionId = Context.ConnectionId;
var user = Users.GetOrAdd(profileId, _ => new User
{
ProfileId = profileId,
ConnectionIds = id
});
lock (user.ConnectionIds)
{
Groups.Add(connectionId, user.ProfileId);
}
return base.OnConnected();
}
[HubMethodName("sendNotifications")]
public Task SendNotifications(string id)
{
UserID = id;
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
string query = "SELECT NotificationNumber FROM [dbo].[NotificationStatus] WHERE UserID=" + UserID;
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Notification = null;
DataTable dt = new DataTable();
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
dt.Load(reader);
if (dt.Rows.Count > 0)
{
totalNewMessages = Int16.Parse(dt.Rows[0]["NotificationNumber"].ToString());
}
}
}
User CurrentUser = UserList.FirstOrDefault(i => i.ProfileId == UserID);
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
//return context.Clients.All.RecieveNotification(totalNewMessages);
return context.Clients.Client(Users.Values.FirstOrDefault(i => i.ProfileId == UserID).ConnectionIds).RecieveNotification(totalNewMessages);
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
NotificationHub nHub = new NotificationHub();
nHub.SendNotifications(UserID);
}
}
}
These 2 lines is what I want to focus on.
//return context.Clients.All.RecieveNotification(totalNewMessages);
return context.Clients.Client(Users.Values.FirstOrDefault(i => i.ProfileId == UserID).ConnectionIds).RecieveNotification(totalNewMessages);
If i use the first line it returns the totalNewMessages back to ALL people on the website, but i only want to to return back to the user that requested it.
The second line is my attempt to return the totalNewMessages back tot he specific user, but it dont work.
How can i return the totalNewMessages Back to only the specific user?
I came across this problem a while back. I got it working by returning the messages depending on the user who's logged in
string userName = HttpContext.Current.User.Identity.Name;
return context.Clients.User(userName).RecieveNotification(totalNewMessages);
Also look into parameterized queries to prevent sql injection. There's a handy documentation for what you're trying to achieve here
Do anyone know how to insert data into database only once during runtime? because right now whenever i run my system, the data would be always inserted into the database. its there any way just to insert the data only once even if i have run the program for many times?Here are my codes
public async void getUserName()
{
LiveConnectClient client = new LiveConnectClient(session);
LiveOperationResult operationResultUserID = await client.GetAsync("me");
dynamic resultUserID = operationResultUserID.Result;
userID = resultUserID.id;
//getUserInfo();
Service1Client client1 = new Service1Client();
name = await client1.RetrieveNameAsync(userID);
dob = await client1.RetrieveDOBAsync(userID);
aboutMe = await client1.RetrieveAboutMeAsync(userID);
country = await client1.RetrieveCountryAsync(userID);
email = await client1.RetrieveEmailAddressAsync(userID);
gender = await client1.RetrieveGenderAsync(userID);
//status = await client1.RetrieveUserStatusAsync(userID);
UserImage = await client1.RetrieveUserImgAsync(userID);
vote = await client1.retrieveVotesAsync(userID);
count = await client1.retrievecountLearningstoryAsync(userID);
txtAboutmeDisplay.Text = aboutMe;
txtCountryDisplay.Text = country;
txtDOBDisplay.Text = dob;
txtEmailDisplay.Text = email;
txtGenderDisplay.Text = gender;
txtName.Text = name;
txtvotes.Text = vote;
txtCountDisplay.Text = count.ToString();
int numberofvotes = int.Parse(txtvotes.Text);
if (numberofvotes >=1000)
{
txtstars.Text = "Gold";
}
else if (numberofvotes >= 700)
{
txtstars.Text = "Silver";
}
else if (numberofvotes >= 500)
{
txtstars.Text = "Bronze";
}
//txtstars.Visibility == false;
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
writer.WriteBytes(UserImage);
await writer.StoreAsync();
// Create bitmap image
BitmapImage b = new BitmapImage();
b.SetSource(randomAccessStream);
// Update Image on XAML Page
imgProfilePic.Source = b;
int countstory = int.Parse(txtCountDisplay.Text);
if (countstory >= 7)
{
achievement = await client1.updateachievementAsync(userID, "wisemen");
}
else if (countstory == 6)
{
achievement = await client1.updateachievementAsync(userID, "Smartboy");
}
else if (countstory == 5)
{
achievement = await client1.insertAchievementAsync(userID, "novice");
}
}
My webservice codes
public string insertAchievement(string userid, string achievements)
{
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string insertInterBadges = "Insert into [Achievement] (UserID, Achievement) VALUES " + " (#userid,#achievements)";
SqlCommand cmd = new SqlCommand(insertInterBadges, con);
cmd.Parameters.AddWithValue("#userId", userid);
cmd.Parameters.AddWithValue("#achievements", achievements);
int check = cmd.ExecuteNonQuery();
con.Close();
if (check > 0)
{
return "Success";
}
else
{
return "Fail";
}
}
public string updateachievements(string userid, string achievements)
{
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string updateAchievements = "UPDATE Achievement SET Achievement=#achievement Where UserID=#userid";
SqlCommand cmd = new SqlCommand(updateAchievements, con);
cmd.Parameters.AddWithValue("#userId", userid);
cmd.Parameters.AddWithValue("#achievement", achievements);
int check = cmd.ExecuteNonQuery();
con.Close();
if (check > 0)
{
return "Success";
}
else
{
return "Fail";
}
}
my reference.cs
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService1/updateachievement", ReplyAction = "http://tempuri.org/IService1/updateachievement")]
System.Threading.Tasks.Task<string> updateachievementAsync(string userId, string achievements);
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService1/insertAchievement", ReplyAction = "http://tempuri.org/IService1/insertAchievement")]
System.Threading.Tasks.Task<string> insertAchievementAsync(string userId, string achievements);
public System.Threading.Tasks.Task<string> updateachievementAsync(string userId, string achievements)
{
return base.Channel.insertAchievementAsync(userId, achievements);
}
public System.Threading.Tasks.Task <string> insertAchievementAsync(string userId, string achievements)
{
return base.Channel.insertAchievementAsync(userId, achievements);
}
i am using webservice by the way
By the looks of your code, specifically the UPDATE statement, a single user can only be assigned a single achievement. That limitation makes the easy-way-out of creating a unique index on UserId, AcheivementId not a viable option (this index, if created, would prevent duplicate entries via a SQL insertion error).
The alternative, more correct, solution is to query the table prior to inserting to see if the values already exist:
SELECT COUNT(*) FROM Achievement WHERE UserId = #userId AND Achievement = #achievement
This could be used in a block like:
using (SqlConnection conn = new SqlConnection(connectionString)) {
conn.Open();
bool isNewValue = true;
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Achievement WHERE UserId = #userId AND Achievement = #achievement")) {
cmd.Parameters.AddWithValue("#userId", userid);
cmd.Parameters.AddWithValue("#achievement", achievements);
isNewValue = ((int)cmd.ExecuteScalar() == 0);
}
if (isNewValue) {
// insert user achievement / etc
}
}