Integrate smartcontract with reactjs - reactjs

I want to show data in reactjs application like public variables showing in remixid
enter image description here
when I try to console it's showing
enter image description here
const contractBalance = aw ait contract.getBalance();
const goal = await contract.goal();
const admin = await contract.admin();
const minCont = await contract.minimumContribution();
const nOfCont = await contract.noOfContributors();
const deadline = await contract.deadline();
const numReq = await contract.numRequests();
const raisedAmount = await contract.raisedAmount();
const requestIndex = await contract.requests(0);
console.log('data: ', ethers.utils.formatEther(contractBalance));
console.log('requestIndex > ', requestIndex);
console.log("minCont > " , minCont);
console.log("numReq > " , numReq);
console.log("raisedAmount > " , raisedAmount);
console.log("nOfCont > " , nOfCont);
console.log("admin > " , admin);
console.log("deadline > " , deadline);
please help!
thanks

Because numbers in solidity are treated as BigNumbers.
The most simple way to convert them is by casting them to string:
console.log("minCont > " , minCont.toString());

Related

discord.js v13 How do I verify a guildMember is also a member of another guild?

This is my first post here, sorry in advance if the formatting isn't great.
I'll try to keep it short and sweet. I've made a small bot with discord.js v13 and it's main purpose is to check if new people that join is part of another guild, if they are they get a role and if they had a nickname, they'll get that assigned. The bot is on both servers with full admin rights and the server members intent is enabled. I've tried all sorts of things.
Currently, the code looks like this:
const {other_server_ID, role_ID, text_channel_ID} = require("../config.json");
module.exports = {
name: 'guildMemberAdd',
async execute( member, bot ) {
//Log the newly joined member to console
console.log(member.user.tag + ' has joined the server!');
if(member.bot) return
const text_channel = member.guild.channels.cache.get(text_channel_ID)
const role = member.guild.roles.cache.get(role_ID)
text_channel.send(member.user.tag + ' has joined the server!')
const other_server = bot.guilds.cache.get(other_server_ID)
const other_server_member = other_server.members.cache.get(member.user.id)
console.log(other_server_member)
if (other_server_member === null) text_channel.send(member.user.tag + " was not a member of other_server :(")
else {
text_channel.send(member.user.tag + " was a member of other_server!")
if(!other_server_member.nickname){
text_channel.send(member.tag + " had no nickname on other_server.")
}
else {
await member.setNickname(other_server_member.nickname)
text_channel.send("Their old nickname, " + other_server_member.nickname + ", was returned to them! :)")
}
if(!member.guild.roles.cache.get(role_ID)){
text_channel.send("Role not found.")}
else{
await member.roles.add(role)
text_channel.send("They were given the role " + role.name)
}
}
}
}
As you can see, I am trying to get the other server (it works that far) and then try to grab the user from the other server's cache. Currently I try with get and the user.id but I have also tried:
const other_server_member = other_server.members.cache.find(mem => mem.user.id === member.user.id) || other_server.members.cache.find(mem => mem.user.tag === member.user.tag) || null
I found that code snippet during my search for a solution. So far I always got errors because other_server_member was always undefined/null. I also tried putting await infront of the find/get part.
Thanks for your help and time.
UPDATE:
It works fine with fetch(), code now looks like this:
const {other_server_ID, role_ID, text_channel_ID} = require("../config.json");
module.exports = {
name: 'guildMemberAdd',
async execute( member, bot ) {
//Log the newly joined member to console
console.log(member.user.tag + ' has joined the server!');
if(member.bot) return
const text_channel = member.guild.channels.cache.get(text_channel_ID)
const role = member.guild.roles.cache.get(role_ID)
text_channel.send(member.user.tag + ' has joined the server!')
const other_server = bot.guilds.cache.get(other_server_ID)
const other_server_member = null
try {
other_server_member = await other_server.members.fetch(member.user.id)
} catch (e) {
console.log(e)
return text_channel.send(member.user.tag + " was not a member of " + pluto.name + ".")
}
console.log(other_server_member)
if (other_server_member === null) text_channel.send(member.user.tag + " was not a member of other_server :(")
else {
text_channel.send(member.user.tag + " was a member of other_server!")
if(!other_server_member.nickname){
text_channel.send(member.tag + " had no nickname on other_server.")
}
else {
await member.setNickname(other_server_member.nickname)
text_channel.send("Their old nickname, " + other_server_member.nickname + ", was returned to them! :)")
}
if(!member.guild.roles.cache.get(role_ID)){
text_channel.send("Role not found.")}
else{
await member.roles.add(role)
text_channel.send("They were given the role " + role.name)
}
}
}
}

"This interaction failed" error in command (discord.js)

I'm working on a randominteger slash command in discord.js
It supposed to work like this :
Discord User : /randominteger minimal:1 maximal:10
Bot : (embed)
Random number
4
1 - 10
But instead the bot just says "This interaction failed"
I don't know why the bot says that though.
Can someone explain how it happened and how to fix it?
const { SlashCommandBuilder, SlashCommandStringOption } = require('#discordjs/builders');
const { MessageEmbed } = require('discord.js')
const color = "#ffed47"
module.exports = {
data: new SlashCommandBuilder()
.setName('randominteger')
.setDescription('Replies with a random integer!')
.addIntegerOption(option =>
option.setName('minimal')
.setDescription('The minimal integer for the random integer')
.setRequired(true))
.addIntegerOption(option =>
option.setName('maximal')
.setDescription('The maximal integer for the random integer')
.setRequired(true)),
async execute(interaction) {
const minimal = interaction.options.getInteger("minimal")
const maximal = interaction.options.getInteger("maximal")
minimal = Math.ceil(minimal)
maximal = Math.floor(maximal)
const randomNumber = Math.floor(Math.random() * (maximal+0.1 - minimal) + minimal);
const random = new MessageEmbed()
.setColor(color)
.setTitle("Random Number")
.setDescription(String(randomNumber))
.setFooter(String(minimal) + " - " + String(maximal))
await interaction.reply({embeds : [random]})
},
};
Sorry, I just didn't implement the the command properly xd
It worked.. with a 1 thing, you need to change the minimal and maximal variable to "var" not "const" (so the minimal and maximal variable can be used)

Economy leaderboard command: undefined

I am making a discord economy/currency bot, and this is the leaderboard command. It works, but whenever I run the command !leaderboard, I don't get any of the user's tags, I just get the undefined#0000. I would like my leaderboard command to show the users with the highest amount of currency.
const { MessageEmbed } = require('discord.js');
const db = require('quick.db');
module.exports = {
name: "leaderboard",
description: 'server\'s $ leaderboard',
aliases: ['lb'],
}
module.exports.run = async (message) => {
let money = db.all().filter(data => data.ID.startsWith(`money_`)).sort((a, b) => b.data - a.data);
if (!money.length) {
let noEmbed = new MessageEmbed()
.setAuthor(message.member.displayName, message.author.displayAvatarURL())
.setColor("BLUE")
.setFooter("No leaderboard")
return message.channel.send(noEmbed)
};
money.length = 10;
var finalLb = "";
for (var i in money) {
let currency1;
let fetched = await db.fetch(`currency_${message.guild.id}`);
if (fetched == null) {
currency1 = '🎱'
} else {
currency1 = fetched
}
if (money[i].data === null) money[i].data = 0
finalLb += `**${money.indexOf(money[i]) + 1}. ${message.guild.members.cache.get(money[i].ID.split('_')[1]) ? message.guild.members.cache.get(money[i].ID.split('_')[1]).tag : "undefined#0000"}** - ${money[i].data} ${currency1}\n`;
};
const embed = new MessageEmbed()
.setTitle(message.guild.name)
.setColor("BLUE")
.setDescription(finalLb)
.setTimestamp()
.setFooter('Command: !help for currency commands')
message.channel.send(embed);
}
Try following code:
let money = db.all().filter(data => data.ID.startsWith(`money_${message.guild.id}`)).sort((a, b) => b.data - a.data)
money.length = 10;
var finalLb = "";
for (var i in money) {
finalLb += `**${money.indexOf(money[i])+1}. ${client.users.cache.get(money[i].ID.split('_')[1]) ? client.users.cache.get(money[i].ID.split('_')[1]).tag : "Unknown User#0000"}** - ${money[i].data}\n`;
}
const embed = new Discord.MessageEmbed()
.setAuthor(`Global Coin Leaderboard!`, message.guild.iconURL())
.setColor("#7289da")
.setDescription(finalLb)
.setFooter(client.user.tag, client.user.displayAvatarURL())
.setTimestamp()
message.channel.send(embed);
I personally use above code for my bot and it works pretty well for me.
Try putting the client.login('token') at the bottom of your code. Maybe the bot can't find the user tag's because of that?

DiscordJS Argument Slice

I want to make a image embed via arguments, but I can't set the Title of the embed.
But when I try to run the comand, it errors out and the embed won't appear
if(message.content.startsWith(prefix + "foto-annuncio"))
{
if(!message.member.hasPermission(["MANAGE_MESSAGES"]))
{
/* error message pops out */
}
let argsresult;
let mChannel = message.mentions.channels.first()
message.delete()
argsresult = args.slice(1).join(" ")
let url = args.slice(1).join(" ");
let title = args.slice(2);
message.delete();
const embed = new Discord.RichEmbed ()
.setImage(url)
.setColor('#008000')
.setAuthor('Delta Logistics', 'https://cdn.discordapp.com/attachments/604963851561336832/665504827044003860/zoom_delta_6_discord.png')
.setTitle(title)
mChannel.send(embed).catch(err => console.log(err));
message.channel.send("Done!");
}
As Giuuliopime said you can't set an array as a title, you prob meant to join the arguments:
const title = args.slice(2).join(" ");

How do I make a command call a different command along with itself?

I don't know how to do this and I have been looking for answer but am unable to find it.
if message.content.startswith('^trivia autostart'):
await client.send_message(message.channel, "Game is starting!\n" +
str(player1) + "\n" + str(player2) + "\n" + str(player3) + "\n" +
str(player4) + "\n" + str(player5) + "\n" + str(player6) )
--
I have this code and i'm trying to make it so it when that code gets run that it calls my ^trivia play command without typing it in chat.
Is this possible?
The solution to that would be defining functions for each command you need to be called globally by your bot. Take the following example:
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('error' => console.log);
bot.on('message', message => {
let prefix = '!';
let sender = message.author;
let msg = message.content;
let cont = msg.split(' ');
let args = cont.slice(1);
let cmd = msg.startsWith(prefix) ? cont[0].slice(prefix.length).toUpperCase() : undefined;
// Ping function
// can be: function pingCommand () {...}
let pingCommand = () => {
message.channel.send(`Pong!\nTime: ${bot.ping} ms`);
}
// Main command
if (cmd === 'PING') {
pingCommand();
}
// Calling command in another command
if (cmd === 'TEST') {
message.channel.send('Running a ping test on the bot');
pingCommand();
}
});
bot.login(token);
Hope you understand how it would work

Resources