open a URL in Discord using JDA? - discord-jda

I am using the JDA API for Java Discord bots. When a user click on a button, it should open a url. But I don't know how to do this. Can someone help me?

You need a SlashListener for create a Message with a button. This script looks to see if someone types /sendLink and then sends back a button
public class SlashListener extends ListenerAdapter {
public void onSlashCommand(SlashCommandEvent event) {
if (event.getName().equals("sendLink")) {
event.reply("Click the button to open Link")
.addActionRow(
Button.link("https://github.com/DV8FromTheWorld/JDA", "GitHub").queue();
}
}
}
But remember to register the SlashCommand and the Listener in the MainClass

Related

How can I have one page subscribe to messages from multiple different pages using Xamarin MessagingCenter?

Here's the situation that I have. On DeckPage, I subscribe to a message like this:
public void OnAppearing()
{
RefreshLables();
MessagingCenter.Subscribe<CardFormatPageViewModel>(
this,
"RefreshDeckConfigsLabels",
sender => { RefreshLables(); });
}
private void RefreshLables()
{
-- refresh code
}
public void OnDisappearing()
{
MessagingCenter.Unsubscribe<CardFormatPageViewModel>(this,"RefreshDeckConfigsLabels");
}
This works good and I am sending a message from the CardFormatPageViewModel like this:
MessagingCenter.Send(this, "RefreshDeckConfigsLabels");
I also want to be able to send messages to DeckPage from OptionPageViewModel:
MessagingCenter.Send(this, "RefreshDeckConfigsLabels");
Plus from more models.
Is there a way I can code the subscribe so I can subscribe to one common message that will be accepted if it comes from CardFormatPageViewModel, OptionPageViewModel or the other View Models that need to make it refresh or do I have to do a subscription for every one of them?
sure
MessagingCenter.Send<object>(this, "RefreshDeckConfigsLabels");
and
MessagingCenter.Subscribe<object>(
this,
"RefreshDeckConfigsLabels",
sender => { RefreshLables(); });

How to create a never ending background service in Xamarin.Forms?

I am monitoring the user's location every 15 minutes and I just want the application to continue sending the location even if the user closes the application in the taskbar.
I tried this sample but it's in Xamarin.Android https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services i have to create a dependencyservice but i don't know how.
i have to create a dependencyservice but i don't know how.
First, create an Interface in the Xamarin.forms project:
public interface IStartService
{
void StartForegroundServiceCompat();
}
And then create a new file let's call it itstartServiceAndroid in xxx.Android project to implement the service you want:
[assembly: Dependency(typeof(startServiceAndroid))]
namespace DependencyServiceDemos.Droid
{
public class startServiceAndroid : IStartService
{
public void StartForegroundServiceCompat()
{
var intent = new Intent(MainActivity.Instance, typeof(myLocationService));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
MainActivity.Instance.StartForegroundService(intent);
}
else
{
MainActivity.Instance.StartService(intent);
}
}
}
[Service]
public class myLocationService : Service
{
public override IBinder OnBind(Intent intent)
{
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
// Code not directly related to publishing the notification has been omitted for clarity.
// Normally, this method would hold the code to be run when the service is started.
//Write want you want to do here
}
}
}
Once you want to call the StartForegroundServiceCompat method in Xamarin.forms project, you can use:
public MainPage()
{
InitializeComponent();
//call method to start service, you can put this line everywhere you want to get start
DependencyService.Get<IStartService>().StartForegroundServiceCompat();
}
Here is the document about dependency-service
For iOS, if the user closes the application in the taskbar, you will no longer be able to run any service. If the app is running, you can read this document about ios-backgrounding-walkthroughs/location-walkthrough
You might want to have a look at Shiny by Allan Ritchie. It's currently in beta but I would still suggest using it, as it will save you a lot of trouble writing this code yourself. Here's a blog post by Allan, explaining what you can do with Shiny in terms of background tasks - I think Scheduled Jobs are the thing you're looking for.

Capture android TV remote keycode

My android app has a function to detect all the keycodes for TV remote.
I tried onKeyDown(up) API to detect when the user press the TV remote and override dispatchKeyEvent method too. It works except some keys : mute, volume, home, back. how can I detect those keys?
Thanks,
The system has the privilege to control those keys, so I have to find some source code to use.
I final resolved this problem. In general, AOSP dispose a led service (led controller):
public LedService() {
}
public IBinder onBind(Intent intent) {
this.handlePowerStateChanged(0);
return new LedService.LedServiceWrapper();
}
public void handlePowerStateChanged(int state) {
if (DEBUG) {
Log.d(TAG, "handlePowerStateChanged: " + state);
}
}
public void handleKeyEvent(KeyEvent event) {
if (DEBUG) {
Log.d(TAG, "handleKeyEvent: " + event);
}
}
This class use HAL to go down the level and communicate directly with system. I extend this class in my own class and use handleKeyEvent method.
Cheers,
Check the KeyEvent constants, and by the help of the following SO references.
Detect home button press in android
Check if back key was pressed in android?
Android: How to get app to remember if mute button has been pressed or not
Android - Volume Buttons used in my application

Wait for Even type Activity in a waterfallstep dialog (bot framework 4.0)

It's possible to wait and receive an Event type activity in a waterfall step dialog. I use directline 3.0 and inside a dialog flow I send an event from the bot to the client. After i would like to send an event from the client to the bot as an answer to previous send. If i use prompt await dc.Prompt("waitEvent",activity) where waitEvent is a textprompt and i answer with a message it works fine but I would like to answer to an event with an event. I was thinking that i could write a custom prompt but i didn't find documentation and obviously I could manage the conversation flow but I prefer use Dialogs where possible
You can use the ActivityPrompt abstract class to build an "EventActivityPrompt" class.
There aren't any BotFramework samples of this usage yet, but there are new tests written by the BotFramework team that you can use as an example.
To create your own EventActivityPrompt, you just need to implement the ActivityPrompt like so:
public class EventActivityPrompt : ActivityPrompt
{
public EventActivityPrompt(string dialogId, PromptValidator<Activity> validator)
: base(dialogId, validator)
{
}
}
The core difference between an ActivityPrompt and other Prompts (besides its abstract status) is that ActivityPrompts require a PromptValidator<Activity>, in order to validate the user input.
The next step is to create your validator. Here is the example:
async Task<bool> _validator(PromptValidatorContext<Activity> promptContext, CancellationToken cancellationToken)
{
var activity = promptContext.Recognized.Value;
if (activity.Type == ActivityTypes.Event)
{
if ((int)activity.Value == 2)
{
promptContext.Recognized.Value = MessageFactory.Text(activity.Value.ToString());
return true;
}
}
else
{
await promptContext.Context.SendActivityAsync("Please send an 'event'-type Activity with a value of 2.");
}
return false;
}

Vaadin 8.4.0 Modal for save confirmation after grid buffer save

We are using a grid to present some data. This grid is not using a data provider but setting its items.
We are working on buffered modd, but we still want to show a modal informing what are we about to save, with the posibility to save or cancel.
SaveEditor method has been removed from grid class in our current version (8.4.0), so cant do it that way.
I have come to a close solution but with some remaining problems.
I have extended grid component to be able to create my own editor:
public class MyGridComponent extends Grid<MyData> {
public MyGridComponent (Class<MyData> beanType) {
super(beanType);
}
#Override
protected Editor<MyData> createEditor() {
return new MyGridEditor(this.getPropertySet());
}
}
On my editor I have overriden following methods:
#Override
protected void doEdit(OutcomeWagerLimit bean) {
copyMyBean = bean;
super.doEdit(bean);
}
#Override
public boolean save() {
String desc = copyMyBean.getDescription();
StringBuilder captionBuilder = new StringBuilder()
.append("Save ")
.append(desc)
.append("?");
StringBuilder messageBuilder = new StringBuilder()
.append("Do you really want to save ")
.append(desc)
.append("?");
openConfirmMsgBox(captionBuilder.toString(), messageBuilder.toString(),() -> super.save(), ()->super.cancel());
return true;
}
With this code clicking on save opens my confirmation modal. If clicking on save, everything works flawlessly, but clicking on my modal cancel which will call to EditorImpl.cancel() method, acts in a weird way. Clicking cancel on my modal will close edition mode, but if I edit again any other row (double clicking on it) grid's save and cancel buttons (not the modal ones) stop working. Not launching any request from client to vaadin's servlet.
Does anyone know any possible solution to this or a better way to reach what I'm trying to achieve?
Thanks in advance
Morning,
Just managed to do it. Since not using dataprovider but normal list, I am the one responsible of saving data in other saveEventListener. That is the moment to present modal and in the "ok" case persist it in database.
So there is no need to override EditorImpl save method and do it in a saveEventListener.
Thanks

Resources